package com.navinfo.outdoor.base;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.navinfo.outdoor.R;
import com.kongzue.dialog.interfaces.OnBackClickListener;
import com.kongzue.dialog.v3.WaitDialog;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.util.BackHandlerHelper;
import com.navinfo.outdoor.util.FragmentBackHandler;
import com.navinfo.outdoor.util.NetWorkUtils;

import java.util.Timer;
import java.util.TimerTask;


public abstract class BaseFragment extends Fragment implements FragmentBackHandler {

    private AlertDialog alertDialog;
    public View mView;
    protected FragmentManager supportFragmentManager;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = LayoutInflater.from(getActivity()).inflate(getLayout(), container, false);
        return mView;

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initMvp();
        initData();
        initView();
        initEvent();
    }

    @Override
    public boolean onBackPressed() {
        return false;
    }

    public void initEvent() {}

    protected <T extends View> T findViewById(@IdRes int id) {
        return getView().findViewById(id);
    }


    protected abstract int getLayout();

    public void showLoadingDialog() {
        if (NetWorkUtils.iConnected(getContext())) { // 当前网络可用
            alertDialog = new AlertDialog.Builder(getActivity()).create();
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
            alertDialog.setCancelable(false);
            alertDialog.setOnKeyListener((dialog, keyCode, event) -> {
                if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_BACK)
                    return true;
                return false;

            });
            //loading样式
            View view = LayoutInflater.from(getContext()).inflate(R.layout.loading, null);
            alertDialog.setView(view);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
            //initTimer();
        } else {
            Toast.makeText(getActivity(), "网络不可用", Toast.LENGTH_SHORT).show();
        }
    }
    public void showFileLoadingDialog() {
        if (NetWorkUtils.iConnected(getContext())) { // 当前网络可用
            alertDialog = new AlertDialog.Builder(getActivity()).create();
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
            alertDialog.setCancelable(false);
            alertDialog.setOnKeyListener((dialog, keyCode, event) -> {
                if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_BACK)
                    return true;
                return false;

            });
            //loading样式
            View view = LayoutInflater.from(getContext()).inflate(R.layout.loading, null);
            alertDialog.setView(view);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            Toast.makeText(getActivity(), "网络不可用", Toast.LENGTH_SHORT).show();
        }
    }

    private void initTimer() {
        Timer timer = new Timer(true);
        TimerTask timerTask = new TimerTask() {
            int countTime = 20;

            @Override
            public void run() {
                if (countTime > 0) {
                    countTime--;
                }
                if (countTime == 1) {
                    dismissLoadingDialog();
                }

            }
        };
        timer.schedule(timerTask, 1000, 1000);
    }


    public void setLoadingDialogText(String s) {
        //给loading 添加文字
        TextView view = alertDialog.findViewById(R.id.progressBar_tx);
        view.setText(s);
    }

    public void dismissLoadingDialog() {
        if (null != alertDialog && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }

    public void dismissDialog() {
        if (!NetWorkUtils.iConnected(getContext())) { // 当前网络bu可用
            Toast.makeText(getActivity(), "请检查网络", Toast.LENGTH_SHORT).show();
            dismissLoadingDialog();
        }
    }

    protected void initMvp() {}

    protected void initData() {}

    protected void initView() {}

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
