package com.emato.ich.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;

import com.emato.ich.device.DeviceControl;
import com.emato.ich.local.LocalStorage;
import com.emato.ich.message.ICHPublishClient;
import com.emato.ich.utils.Log;
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 androidx.navigation.fragment.NavHostFragment;

import com.emato.ich.MainActivity;
import com.emato.ich.R;
import com.emato.ich.databinding.FragmentSendMainBinding;
import com.emato.ich.utils.ButtonUtils;
import com.emato.ich.utils.LoggingUtils;
import com.emato.ich.utils.NavUtils;
import com.emato.ich.utils.TimeOutUtils;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CountDownLatch;

/**
 * 投递员主页
 */
public class SendMainFragment extends Fragment {

    private FragmentSendMainBinding binding;
    private static final String TAG = SendMainFragment.class.getName();
    private CountDownTimer timer;
    private long start;

    @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 = FragmentSendMainBinding.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。");

        // 获取登录人员信息
        String loginPhone = LocalStorage.getInstance().getSession().getPhone();
        // 根据登录账号判断是否为管理员
        if(!"18888888881".equals(loginPhone)){
            // 如果不是 则设置为隐藏
            view.findViewById(R.id.upload_cab_info_btn).setVisibility(View.GONE);
            view.findViewById(R.id.query_upload_cab_info_btn).setVisibility(View.GONE);
        }
//        ColorConstant.setButtonColor(binding.returnBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
        // 去除异常页数据
        try {
            MainActivity activity = ((MainActivity) getActivity());
            activity.getBundleMap().remove(ExceptionFragment.class.getName());
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 去除异常页数据异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 投递员主页去除异常页数据异常! ", e);
        }
        try {
            MainActivity activity = ((MainActivity) getActivity());
            timer = TimeOutUtils.timeout(activity, SendMainFragment.this, binding.timeout, 60);
            timer.start();
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 投递员主页倒计时出现异常! ", e);
        }
        // 输入快件信息
        binding.sendConfirmBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_sendMainFragment_to_chooseCabinetFragment);
        });
        // 异常处理
        binding.exceptionHandleBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_sendMainFragment_to_exceptionFragment);
        });
        binding.returnBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_sendMainFragment_to_sendFragment);
        });
        // 全开柜门
        binding.uploadCabInfoBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }

            try {
                MainActivity activity = ((MainActivity) getActivity());
                ICHPublishClient ichPublishClient = ICHPublishClient.getInstance();
                if (activity != null) {
                    CountDownLatch countDownLatch = new CountDownLatch(10);
                    for (int i = 1; i <= 10; i++) {
                        // 先全开柜门
                        DeviceControl.queryLockListInfo(i,null,countDownLatch);
                    }

                    //activity.cabinetInfoReport(ichPublishClient);
                }else {
                    Log.e(TAG, "未获取到页面！! ");
                    LoggingUtils.sendErrorLog("上报数据时未获取到页面! ",new Throwable("上报数据时未获取到页面"));
                }
            }catch (Exception e){
                Log.e(TAG, "上报数据失败！!，系统异常：",e);
                LoggingUtils.sendErrorLog("上报数据失败！！，系统异常：",e);
            }

            if (null != timer) {
                timer.cancel();
            }
        });
        // 上传数据
        binding.queryUploadCabInfoBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }

            try {
                MainActivity activity = ((MainActivity) getActivity());
                ICHPublishClient ichPublishClient = ICHPublishClient.getInstance();
                if (activity != null) {
                    activity.cabinetInfoReport(ichPublishClient);
                }else {
                    Log.e(TAG, "未获取到页面！! ");
                    LoggingUtils.sendErrorLog("上报数据时未获取到页面! ",new Throwable("上报数据时未获取到页面"));
                }
            }catch (Exception e){
                Log.e(TAG, "上报数据失败！!，系统异常：",e);
                LoggingUtils.sendErrorLog("上报数据失败！！，系统异常：",e);
            }

            if (null != timer) {
                timer.cancel();
            }
        });

    }


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