package com.edufound.reader.popwindow;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.edufound.reader.R;
import com.edufound.reader.application.EApplication;
import com.edufound.reader.bean.RecordResultBean;
import com.edufound.reader.cusview.GridRadioGroup;
import com.edufound.reader.listener.PopUtilClickListener;
import com.edufound.reader.listener.PopupRecordStatusListener;
import com.edufound.reader.presenter.PopWindowPresneter;
import com.jakewharton.rxbinding4.view.RxView;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.functions.Consumer;

public class PopWindowUtil {
    private static PopWindowPresneter mPresenter;
    private static PopupWindow mPopupWindow;


    private static void initPresenter() {
        if (mPresenter == null) {
            mPresenter = new PopWindowPresneter();
        }
    }

    private static boolean checkWindowShoing() {
        if (mPopupWindow == null) {
            return false;
        }
        return mPopupWindow.isShowing();
    }

    public static void hidePopupWindow() {
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    public static void showExitAppWindow(Context context, View parent) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_app, null);
        dialog_view.setFocusable(true);

        ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_app_image);
        FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_app_ok);
        FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_app_cancel);
        setClickListener(exit, o -> {
            EApplication.killAppProcess(context);
        });
        setClickListener(cancel, o -> {
            mPopupWindow.dismiss();
        });
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public static void showSelectGradeWindow(Context context, View parent, int checked, PopUtilClickListener listener) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_select_grade, null);
        dialog_view.setFocusable(true);
        FrameLayout btn_ok = dialog_view.findViewById(R.id.popupwindow_select_ok);
        GridRadioGroup gridRadioGroup = dialog_view.findViewById(R.id.popupwindow_select_grade_gridgroup);
        int id = 0;
        final String[] checkText = {String.valueOf(id)};
        switch (checked) {
            case 0:
                id = R.id.popupwindow_select_grade_one;
                checkText[0] = "一年级";
                break;
            case 1:
                id = R.id.popupwindow_select_grade_two;
                checkText[0] = "二年级";
                break;
            case 2:
                id = R.id.popupwindow_select_grade_three;
                checkText[0] = "三年级";
                break;
            case 3:
                id = R.id.popupwindow_select_grade_four;
                checkText[0] = "四年级";
                break;
            case 4:
                id = R.id.popupwindow_select_grade_pre;
                checkText[0] = "学前";
                break;
        }


        gridRadioGroup.setOnCheckedChangeListener(new GridRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(GridRadioGroup group, int checkedId) {
                Logger.e("checkedId:" + checkedId);
                group.check(checkedId);
                switch (checkedId) {
                    case R.id.popupwindow_select_grade_one:
                        //一年级
                        checkText[0] = "一年级";
                        break;
                    case R.id.popupwindow_select_grade_two:
                        checkText[0] = "二年级";
                        //二年级
                        break;
                    case R.id.popupwindow_select_grade_three:
                        checkText[0] = "三年级";
                        //三年级
                        break;
                    case R.id.popupwindow_select_grade_four:
                        checkText[0] = "四年级";
                        //四年级
                        break;
                    case R.id.popupwindow_select_grade_pre:
                        //学前
                        checkText[0] = "学前";
                        break;
                }

            }
        });
        gridRadioGroup.check(id);
        setClickListener(btn_ok, o -> {
            mPopupWindow.dismiss();
            listener.clickSubmit(checkText[0]);
        });
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public static void showExitLoginWindow(Context context, View parent, PopUtilClickListener listener) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_login, null);
        dialog_view.setFocusable(true);
        ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_login_image);
        FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_login_ok);
        FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_login_cancel);
        setClickListener(exit, o -> {
            listener.clickSubmit(null);
        });
        setClickListener(cancel, o -> {
            mPopupWindow.dismiss();
            listener.clickCancel();
        });
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public static void showCancellationAccountWindow(Context context, View parent, PopUtilClickListener listener) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_cancellation_account, null);
        dialog_view.setFocusable(true);
        FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_cancellation_account_ok);
        FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_cancellation_account_cancel);
        setClickListener(exit, o -> {
            listener.clickSubmit(null);
        });
        setClickListener(cancel, o -> {
            mPopupWindow.dismiss();
            listener.clickCancel();
        });
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public static void showBindWeChatWindow(Context context, View parent, PopUtilClickListener listener) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_bind_wechat, null);
        dialog_view.setFocusable(true);
        ImageView back = dialog_view.findViewById(R.id.popupwindow_bindwechat_back);
        setClickListener(back, o -> {
            mPopupWindow.dismiss();
            listener.clickCancel();
        });
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public static void showRecordStatusWindow(Context context, View parent, RecordResultBean bean, PopupRecordStatusListener listener) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_record_status, null);
        dialog_view.setFocusable(true);
        dialog_view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        ImageView back = dialog_view.findViewById(R.id.popupwindow_record_status_close);
        setClickListener(back, o -> {
            mPopupWindow.dismiss();
        });
        mPresenter.initRecordStatusWindow(context, dialog_view, bean, listener);
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(false);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public static void showMyOrderWindow(Context context, View parent) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_myorder, null);
        dialog_view.setFocusable(true);
        dialog_view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        ImageView back = dialog_view.findViewById(R.id.popupwindow_myorder_back);
        setClickListener(back, o -> {
            mPopupWindow.dismiss();
        });
        mPresenter.myOrderWindowInit(context, dialog_view);
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(false);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public static void showRecordRemovedWindow(Context context, View parent, PopUtilClickListener listener) {
        initPresenter();
        if (checkWindowShoing()) {
            return;
        }
        View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_record_remove, null);
        dialog_view.setFocusable(true);
        FrameLayout ok = dialog_view.findViewById(R.id.popupwindow_record_remove_ok);
        FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_record_remove_cancel);
        setClickListener(ok, o -> {
            mPopupWindow.dismiss();
            listener.clickSubmit(null);
        });

        setClickListener(cancel, o -> {
            mPopupWindow.dismiss();
            listener.clickCancel();
        });
        mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    private static void setClickListener(View view, Consumer onNext) {
        RxView.clicks(view).throttleFirst(2, TimeUnit.SECONDS).subscribe(onNext);
    }
}