package com.emato.ich.fragment;

import android.os.Bundle;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.emato.ich.entity.BzCabBizCfg;
import com.emato.ich.enums.TakeButtonShowEnum;
import com.emato.ich.enums.TakeQrCodeShowEnum;
import com.emato.ich.utils.JacksonUtils;
import com.emato.ich.utils.Log;

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.fragment.app.Fragment;

import com.emato.ich.MainActivity;
import com.emato.ich.R;
import com.emato.ich.contant.SystemConfigConstant;
import com.emato.ich.databinding.FragmentMainBinding;
import com.emato.ich.utils.ButtonUtils;
import com.emato.ich.utils.LoggingUtils;
import com.emato.ich.utils.NavUtils;
import com.emato.ich.utils.StringUtils;
import com.emato.ich.utils.TimeOutUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hacknife.carouselbanner.CarouselBanner;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 主页
 */
public class MainFragment extends Fragment {

    private static final String TAG = MainFragment.class.getName();
    private FragmentMainBinding binding;
    private long start;
    CarouselBanner banner;
    private CountDownTimer timer;
    @Override
    public View onCreateView(
            @NotNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        start = System.currentTimeMillis();
        binding = FragmentMainBinding.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。");
        MainActivity activity = ((MainActivity) getActivity());
        try {

            timer = TimeOutUtils.timeout(activity, MainFragment.this, binding.timeout, 20);
            timer.start();
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 取件码输入页面倒计时出现异常! ", e);
        }
        super.onViewCreated(view, savedInstanceState);

        String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);
        String takeQrCodeShow = activity.getConfigMap().get(SystemConfigConstant.take_qr_code_show);
        String takeButtonShow = activity.getConfigMap().get(SystemConfigConstant.take_button_show);

        // 取件二维码不展示  取件码取件展示
        if (TakeQrCodeShowEnum.item_1.getItem().equals(takeQrCodeShow) && !TakeButtonShowEnum.item_1.getItem().equals(takeButtonShow)){
            view.findViewById(R.id.fast_take_send).setVisibility(View.GONE);
            view.findViewById(R.id.fast_take_send_background).setVisibility(View.GONE);
            view.findViewById(R.id.take_btn).setVisibility(View.VISIBLE);

            view.findViewById(R.id.take_btn).setTranslationX(-53);
            view.findViewById(R.id.send_btn).setTranslationX(-53);
            view.findViewById(R.id.deliverer_btn).setTranslationX(-53);
        }else if (!TakeQrCodeShowEnum.item_1.getItem().equals(takeQrCodeShow) && TakeButtonShowEnum.item_1.getItem().equals(takeButtonShow)){
            // 取件二维码展示  取件码取件不展示
            view.findViewById(R.id.fast_take_send).setVisibility(View.VISIBLE);
            view.findViewById(R.id.fast_take_send_background).setVisibility(View.VISIBLE);
            view.findViewById(R.id.take_btn).setVisibility(View.GONE);
            view.findViewById(R.id.send_btn).setTranslationY(50);
        }else {
            // 取件二维码展示  取件码取件展示
            view.findViewById(R.id.fast_take_send).setVisibility(View.VISIBLE);
            view.findViewById(R.id.fast_take_send_background).setVisibility(View.VISIBLE);
            view.findViewById(R.id.take_btn).setVisibility(View.VISIBLE);
        }
        if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {

            try {
                ImageView imageView = view.findViewById(R.id.fast_take_send);
                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);
            }

        }

        // 取出缓存 重新加载图片
        // 获取轮播图数组字符串
        String cabImgListStr = activity.getConfigMap().get(SystemConfigConstant.CAB_IMG_LIST);
        // 转为 List
        List<String> cabImgList = new ArrayList<>();
        ObjectMapper objectMapper = JacksonUtils.objectmapper;
        if(cabImgListStr != null){
            try {
                cabImgList = objectMapper.readValue(cabImgListStr, new TypeReference<List<String>>() {
                });
            } catch (JsonProcessingException e) {
                Log.e(TAG, "获取轮播图异常: Glide设置图片异常! ", e);
                LoggingUtils.sendErrorLog("业务异常: 获取缓存轮播图异常: Glide设置轮播图失败! ", e);
            }
            banner = view.findViewById(R.id.banner);
            if(!cabImgList.isEmpty() && cabImgList.size() > 0){
                banner.initBanner(cabImgList);
            }else {
                Log.e(TAG, "轮播图为空！: Glide设置图片异常! ");
                LoggingUtils.sendErrorLog("轮播图为空！: Glide设置轮播图失败! ",null);
            }
        }

        Bundle bundle = new Bundle();
        // 我要寄快递页面跳转
        binding.sendBtn.setOnClickListener(view12 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }


            bundle.putString("pageType","expressDelivery");
            activity.getBundleMap().put(MainFragment.class.getName(), bundle);
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_mainFragment_to_chooseCabinetFragment);
        });
        // 取件页面跳转 德合隐藏
        binding.takeBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_mainFragment_to_takeFragment);
        });
        // 派件员页面跳转
        binding.delivererBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            bundle.putString("pageType","collectMaterials");
            activity.getBundleMap().put(MainFragment.class.getName(), bundle);
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_mainFragment_to_courier_fragment);
        });
    }

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

}