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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.FragmentCourierBinding;
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 CourierFragment extends Fragment {

    private static final String TAG = CourierFragment.class.getName();
    private FragmentCourierBinding 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 = FragmentCourierBinding.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);
        try {
            MainActivity activity = ((MainActivity) getActivity());
            timer = TimeOutUtils.timeout(activity, CourierFragment.this, binding.timeout, 60);
            timer.start();
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 取件页面倒计时出现异常! ", e);
        }

        MainActivity activity = (MainActivity) getActivity();
        String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);

/*        if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
            // todo:这里可改为从后端获取寄件二维码图片 
            ICSPClient.getWeChatMPImage(url, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    Log.e(TAG, "onFailure: 请求寄件二维码图片错误! 网络异常! ", e);
                    LoggingUtils.sendErrorLog("业务异常: 请求寄件二维码图片错误! 网络异常!", e);
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    getActivity().runOnUiThread(() -> {
                        try {
                            InputStream is = response.body().byteStream();
                            final Drawable d = Drawable.createFromStream(is, "src");
                            binding.expressDeliveryQrCodeImage.setBackground(d);
                            is.close();
                        } catch (Exception e) {
                            Log.e(TAG, "onResponse: 设置图片异常! ", e);
                            LoggingUtils.sendErrorLog("业务异常: 设置图片异常! ", e);
                        }
                    });
                }
            });
        }*/


        binding.storeSendBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            bundle.putString("loginType","store");
            activity.getBundleMap().put(CourierFragment.class.getName(), bundle);
            NavUtils.navigate(this, R.id.action_courierFragment_to_sendKeyBoardFragment);

        });

        binding.courierSendBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            bundle.putString("loginType","courier");
            activity.getBundleMap().put(CourierFragment.class.getName(), bundle);
            NavUtils.navigate(this, R.id.action_courierFragment_to_sendKeyBoardFragment);

        });
        binding.collectMaterial.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            bundle.putString("loginType","collect");
            activity.getBundleMap().put(CourierFragment.class.getName(), bundle);
            NavUtils.navigate(this, R.id.action_courierFragment_to_sendKeyBoardFragment);

        });
        binding.returnBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_courierFragment_to_mainFragment);
        });
    }


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

}
