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.FragmentSendSuccessBinding;
import com.emato.ich.local.LocalStorage;
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 SendSuccessFragment extends Fragment {

    private static final String TAG = SendSuccessFragment.class.getName();
    private FragmentSendSuccessBinding binding;
    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 = FragmentSendSuccessBinding.inflate(inflater, container, false);

        return binding.getRoot();
    }


    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
//        ColorConstant.setButtonColor(binding.returnMainBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
        try {
            MainActivity activity = ((MainActivity) getActivity());
            timer = TimeOutUtils.timeout(activity, SendSuccessFragment.this, binding.timeout, 60);
            timer.start();
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 投递成功页面倒计时出现异常! ", e);
        }

        // 继续投递
        binding.continueSendBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            timer.cancel();
            // 继续投递重新计算异常处理次数
            LocalStorage.getInstance().getSession().setException(true);
            NavHostFragment.findNavController(SendSuccessFragment.this)
                    .navigate(R.id.action_sendSuccessFragment_to_chooseCabinetFragment);
        });

        // 退出账号
        binding.exitAccountBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            // 清空Session
            MainActivity activity = (MainActivity) getActivity();
            LocalStorage.getInstance().cleanSession(activity);
            timer.cancel();
            NavHostFragment.findNavController(SendSuccessFragment.this)
                    .navigate(R.id.action_sendSuccessFragment_to_mainFragment);
        });

        binding.returnMainBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            timer.cancel();
            MainActivity activity = (MainActivity) getActivity();
            LocalStorage.getInstance().cleanSession(activity);
            NavHostFragment.findNavController(SendSuccessFragment.this)
                    .navigate(R.id.action_sendSuccessFragment_to_mainFragment);
        });

    }

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