package com.emato.ich.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
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.TimeOutUtils;

import org.jetbrains.annotations.NotNull;

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

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

    @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) {

        binding = FragmentSendMainBinding.inflate(inflater, container, false);

        return binding.getRoot();
    }


    public  void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
//        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;
            }
            timer.cancel();
            NavHostFragment.findNavController(SendMainFragment.this)
                    .navigate(R.id.action_sendMainFragment_to_chooseCabinetFragment);
        });
        // 异常处理
        binding.exceptionHandleBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            timer.cancel();
            NavHostFragment.findNavController(SendMainFragment.this)
                    .navigate(R.id.action_sendMainFragment_to_exceptionFragment);
        });
        binding.returnBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            timer.cancel();
            NavHostFragment.findNavController(SendMainFragment.this)
                    .navigate(R.id.action_sendMainFragment_to_sendFragment);
        });
    }


    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();
        binding = null;
    }
}
