package com.emato.ich.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.emato.ich.MainActivity;
import com.emato.ich.R;
import com.emato.ich.contant.SystemConfigConstant;
import com.emato.ich.databinding.FragmentScreenProtectBinding;
import com.emato.ich.databinding.FragmentScreenProtectBinding;
import com.emato.ich.utils.ButtonUtils;
import com.emato.ich.utils.Log;
import com.emato.ich.utils.LoggingUtils;
import com.emato.ich.utils.NavUtils;
import com.emato.ich.utils.TimeOutUtils;

import org.jetbrains.annotations.NotNull;

/**
 * 我是派送员页面
 */
public class ScreenProtectFragment extends Fragment {

    private static final String TAG = ScreenProtectFragment.class.getName();
    private FragmentScreenProtectBinding binding;
    private CountDownTimer timer;
    private long start;
    private Bundle bundle = new Bundle();
    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        start = System.currentTimeMillis();
        binding = FragmentScreenProtectBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        long end = System.currentTimeMillis();
        Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
        super.onViewCreated(view, savedInstanceState);

//        ColorConstant.setButtonColor(binding.returnBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
        MainActivity activity = (MainActivity) getActivity();
        String url = activity.getConfigMap().get(SystemConfigConstant.screen_protect_img_url);
        String cabSerialNo = activity.getConfigMap().get(SystemConfigConstant.cab_serial_no);
        // 将图片设置好
        try {
            binding.screenProtectTitle.setText(cabSerialNo);
            ImageView imageView = view.findViewById(R.id.screen_protect_img);
            Glide.with(view).load(url)
                    .skipMemoryCache(true)//跳过内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不缓冲disk硬盘中
                    .into(imageView);
        } catch (Exception e) {
            Log.e(TAG, "onResponse: Glide设置屏保图片异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: Glide请求屏保图片异常! ", e);
        }

        binding.screenProtectImg.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            // 直接跳转首页
            NavUtils.navigate(this, R.id.action_screenProtectFragment_to_mainFragment);
        });
    }


    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
        binding = null;
        Log.i(TAG, TAG + "被销毁。。。");
    }

}
