package com.emato.ich.utils;

import android.os.CountDownTimer;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.emato.ich.MainActivity;
import com.emato.ich.R;
import com.emato.ich.fragment.ChooseCabinetFragment;
import com.emato.ich.fragment.ExceptionFragment;
import com.emato.ich.fragment.InputInfoFragment;
import com.emato.ich.fragment.SendFragment;
import com.emato.ich.fragment.SendInfoConfirmFragment;
import com.emato.ich.fragment.SendMainFragment;
import com.emato.ich.fragment.SendSuccessFragment;
import com.emato.ich.fragment.TakeCodeFragment;
import com.emato.ich.fragment.TakeFragment;
import com.emato.ich.fragment.TakeSuccessFragment;
import com.emato.ich.local.LocalStorage;


public class TimeOutUtils {

    public static CountDownTimer timeout(MainActivity mainActivity, Fragment fragment, TextView textView, int time){
        // TODO 需要action 每一个Fragment都需要一个跳转Main的action  需要判断是哪个Fragment
        return new CountDownTimer(time * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                textView.setText(String.valueOf((millisUntilFinished / 1000)));
            }

            @Override
            public void onFinish() {
                int fragmentAction = getFragmentAction(fragment);
                if (fragmentAction != 0) {
                    // 清空session和页面传值
                    LocalStorage.getInstance().cleanSession(mainActivity);
                    NavHostFragment.findNavController(fragment).navigate(fragmentAction);
                }
            }
        };
    }

    private static int getFragmentAction(Fragment fragment) {

        if (fragment instanceof ChooseCabinetFragment) {
            return R.id.action_chooseCabinetFragment_to_mainFragment;
        } else if (fragment instanceof ExceptionFragment) {
            return R.id.actionExceptionFragment_to_mainFragment;
        } else if (fragment instanceof InputInfoFragment) {
            return R.id.action_inputInfoFragment_to_mainFragment;
        } else if (fragment instanceof SendFragment) {
            return R.id.action_sendFragment_to_mainFragment;
        } else if (fragment instanceof SendInfoConfirmFragment) {
            return R.id.action_sendInfoConfirmFragment_to_mainFragment;
        } else if (fragment instanceof SendMainFragment) {
            return R.id.action_sendMainFragment_to_mainFragment;
        } else if (fragment instanceof SendSuccessFragment) {
            return R.id.action_sendSuccessFragment_to_mainFragment;
        } else if (fragment instanceof TakeCodeFragment) {
            return R.id.action_takeCodeFragment_to_mainFragment;
        } else if (fragment instanceof TakeFragment) {
            return R.id.action_takeFragment_to_mainFragment;
        } else if (fragment instanceof TakeSuccessFragment) {
            return R.id.action_takeSuccessFragment_to_mainFragment;
        }
        return 0;
    }


}
