package com.stu.assistant.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

//import com.bigkoo.pickerview.builder.TimePickerBuilder;
//import com.bigkoo.pickerview.listener.OnTimeSelectListener;
//import com.bigkoo.pickerview.view.TimePickerView;
//import com.wwdw.easytimeapplication.R;
//import com.wwdw.easytimeapplication.contants.AddMoneyBeanType;
//import com.wwdw.easytimeapplication.uitls.AppTools;
//import com.wwdw.easytimeapplication.uitls.CalendarUtil;
//import com.wwdw.easytimeapplication.uitls.MyToast;
//import com.wwdw.easytimeapplication.uitls.ScriptEngineUtils;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.stu.assistant.R;
import com.stu.assistant.base.BaseApplion;
import com.stu.assistant.uitls.AppTools;
import com.stu.assistant.uitls.CalendarUtil;
import com.stu.assistant.uitls.MyToast;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedNoClearUtil;
import com.stu.assistant.uitls.SharedUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DiaogUtil {
    private PopWindowUtils popWindowUtils;

    public static void showInputDialog(final Activity activity, View parent_view, final String title, final OnShowInputLisenter lisenter) {
        final PopWindowUtils popWindowUtils = new PopWindowUtils(activity);
        popWindowUtils.setFocusables(true);
//        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity));
        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity) - AppTools.dip2px(activity, 20));
        popWindowUtils.setOnViewClickListener(new PopWindowUtils.ContentView() {
            @Override
            public void getContentView(View contentView, PopWindowUtils popWindow) {

            }
        }).setConView(R.layout.input_dialog_layout).showInCenter(parent_view);
    }


    public static void showDelDialog(Activity activity, View parent_view, final String msg, final OnNickNameLisenter lisenter) {
        final PopWindowUtils popWindowUtils = new PopWindowUtils(activity);
        popWindowUtils.setFocusables(true);
//        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity));
        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity) - AppTools.dip2px(activity, 20));
        popWindowUtils.setOnViewClickListener(new PopWindowUtils.ContentView() {
            @Override
            public void getContentView(View contentView, final PopWindowUtils popWindow) {
                TextView canTv = contentView.findViewById(R.id.tv_can);
                TextView okTv = contentView.findViewById(R.id.tv_ok);
                final TextView editText = contentView.findViewById(R.id.mark_edit);
                if (!TextUtils.isEmpty(msg)) {
                    editText.setText("确定要删除" + msg + "吗?");
                }
                canTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popWindow.dismiss();
                    }
                });
                okTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popWindow.dismiss();
                        lisenter.OnOkBtn(msg, popWindow);
                    }
                });
            }
        }).setConView(R.layout.select_color_dialog_layout).showInCenter(parent_view);
    }

    public static void showOutAppDialog(Activity activity, View parent_view, final String msg, final OnNickNameLisenter lisenter) {
        final PopWindowUtils popWindowUtils = new PopWindowUtils(activity);
        popWindowUtils.setFocusables(true);
//        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity));
        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity) - AppTools.dip2px(activity, 20));
        popWindowUtils.setOnViewClickListener(new PopWindowUtils.ContentView() {
            @Override
            public void getContentView(View contentView, final PopWindowUtils popWindow) {
                TextView canTv = contentView.findViewById(R.id.tv_can);
                TextView okTv = contentView.findViewById(R.id.tv_ok);
                final TextView editText = contentView.findViewById(R.id.mark_edit);
                editText.setText("确定退出?");
                canTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popWindow.dismiss();
                    }
                });
                okTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popWindow.dismiss();
                        lisenter.OnOkBtn(msg, popWindow);
                    }
                });
            }
        }).setConView(R.layout.select_color_dialog_layout).showInCenter(parent_view);
    }

    public static void showSelectColorDialog(Activity activity, View parent_view, final String msg, final OnNickNameLisenter lisenter) {
        final PopWindowUtils popWindowUtils = new PopWindowUtils(activity);
        popWindowUtils.setFocusables(true);
//        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity));
        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity) - AppTools.dip2px(activity, 20));
        popWindowUtils.setOnViewClickListener(new PopWindowUtils.ContentView() {
            @Override
            public void getContentView(View contentView, final PopWindowUtils popWindow) {
                TextView canTv = contentView.findViewById(R.id.tv_can);
                TextView okTv = contentView.findViewById(R.id.tv_ok);
                RadioGroup radioGroup = contentView.findViewById(R.id.rg);
                canTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popWindow.dismiss();
                    }
                });
                okTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
                        if (checkedRadioButtonId == R.id.rb_1) {
                            SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.isTheme,SharedConfig.isThemeMy);
                            lisenter.OnOkBtn(msg, popWindow);
                        }

                        if (checkedRadioButtonId == R.id.rb_2) {
                            SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.isTheme,SharedConfig.isThemeSys);
                            lisenter.OnOkBtn(msg, popWindow);
                        }
                        popWindow.dismiss();
                    }
                });
            }
        }).setConView(R.layout.select_color_dialog_layout).showInCenter(parent_view);
    }
    public static void showSelectBgDialog(Activity activity, View parent_view, final OnNickNameLisenter lisenter) {
        final PopWindowUtils popWindowUtils = new PopWindowUtils(activity);
        popWindowUtils.setFocusables(true);
//        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity));
        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity) - AppTools.dip2px(activity, 20));
        popWindowUtils.setOnViewClickListener(new PopWindowUtils.ContentView() {
            @Override
            public void getContentView(View contentView, final PopWindowUtils popWindow) {
                TextView canTv = contentView.findViewById(R.id.tv_can);
                TextView okTv = contentView.findViewById(R.id.tv_ok);
                RadioGroup radioGroup = contentView.findViewById(R.id.rg);
                canTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popWindow.dismiss();
                    }
                });
                okTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
                        if (checkedRadioButtonId == R.id.rb_1) {
                            SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.isBg,SharedConfig.isBg_1);
                        }

                        if (checkedRadioButtonId == R.id.rb_2) {
                            SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.isBg,SharedConfig.isBg_2);
                        }
                        if (checkedRadioButtonId == R.id.rb_3) {
                            SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.isBg,SharedConfig.isBg_3);
                        }
                        if (checkedRadioButtonId == R.id.rb_4) {
                            SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.isBg,SharedConfig.isBg_4);
                        }
                        lisenter.OnOkBtn("", popWindow);
                        popWindow.dismiss();
                    }
                });
            }
        }).setConView(R.layout.select_bg_dialog_layout).showInCenter(parent_view);
    }

    public static void showTipDialog(Activity activity, View parent_view, final String msg, final OnNickNameLisenter lisenter) {
        final PopWindowUtils popWindowUtils = new PopWindowUtils(activity);
        popWindowUtils.setFocusables(true);
        popWindowUtils.setEditDissmiss(false);
//        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity));
        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity) - AppTools.dip2px(activity, 20));
        popWindowUtils.setOnViewClickListener(new PopWindowUtils.ContentView() {
            @Override
            public void getContentView(View contentView, final PopWindowUtils popWindow) {
                TextView canTv = contentView.findViewById(R.id.tv_can);
                TextView okTv = contentView.findViewById(R.id.tv_ok);
                final EditText editText = contentView.findViewById(R.id.mark_edit);
                okTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String ymd = new CalendarUtil().getYMD3();
                        Log.i("hx", "--ymd-->" + ymd);
                        int num = Integer.parseInt(ymd.substring(ymd.length() - 1, ymd.length())) + 3;
                        Log.i("hx", "--num-->" + num);
                        String pwd = ymd + ymd.substring(ymd.length() - 2, ymd.length()) + num;
                        Log.i("hx", "--pwd-->" + pwd);

                        String msgNew = editText.getText().toString();
                        if (TextUtils.isEmpty(msgNew)) {
                            MyToast.show("请输入密码");
                            return;
                        }
                        String uidFromBase64 = AppTools.getUidFromBase64(msgNew);
                        Log.i("hx", "--msgNew--->" + msgNew + "-uidFromBase64-->" + uidFromBase64);
                        if (uidFromBase64.equals(pwd) || uidFromBase64.equals("10001")) {
                            popWindowUtils.setEditDissmiss(true);
                            if (uidFromBase64.equals("10001")) {
                                String addDay = new CalendarUtil().setAddDay(new Date(), 365 * 10);
                                SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.initTime, addDay);
                            } else {
                                String addDay = new CalendarUtil().setAddDay(new Date(), 3);
                                SharedNoClearUtil.create(BaseApplion.application).putString(SharedConfig.initTime, addDay);
                            }
                            popWindow.dismiss();
                            lisenter.OnOkBtn(msgNew, popWindow);
                        } else {
                            MyToast.show("密码错误");
                        }
                    }
                });
            }
        }).setConView(R.layout.tip_dialog_layout).showInCenter(parent_view);
    }


    public static void showSelectTimeDialog(Activity activity, final OnSelectLisenter lisenter) {
        Calendar selectedDate = Calendar.getInstance();
        CalendarUtil instance = CalendarUtil.getInstance();
        Date date1 = instance.strToDate2(instance.getYMDHM());
        Date date = instance.setAddYear2(date1, 10);
        Calendar instance1 = Calendar.getInstance();
        instance1.setTime(date);
        selectedDate.setTime(date1);
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        boolean[] booleans = {true, true, true, true, true, false};
        TimePickerView pvTime = new TimePickerBuilder(activity, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v1) {
                String time = dateFormat.format(date);
                lisenter.OnOkBtn(time);
            }
        }).setDecorView((ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content))
                .setSubmitColor(R.color.app_yellow)
                .setCancelColor(R.color.app_yellow)
                .setDate(selectedDate)
                .setType(booleans)
                .isCyclic(true)
                .build();
        pvTime.show();
    }

    public interface OnSelectLisenter {
        void OnOkBtn(String time);
    }


    private static void clickListener(final TextView callTv, final TextView moneyTv) {
        callTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String moneyMsg = getMoney(moneyTv);
                String s = getNum(callTv);
                String msg = moneyMsg + s;
                Log.i("stf", "-clickListener-->" + msg + "-s-->" + s + "-moneyMsg-->" + moneyMsg);
                if (!TextUtils.isEmpty(msg)) {
                    if ((msg.endsWith("+")) || (msg.endsWith("-"))) {
                    } else {
                        setMoney(msg, moneyTv);
                    }
                } else {
                    setMoney(s, moneyTv);
                }
            }
        });
    }

    private static String getMoney(TextView textView) {
        return textView.getText().toString().trim().replace(" ", "");
    }

    private static String getNum(TextView textView) {
        return textView.getText().toString().trim().replace(" ", "");
    }

    private static void setMoney(String msg, TextView textView) {
        textView.setText(msg);
    }


    public interface OnShowInputLisenter {
        void OnOkBtn(String moneyMsg, String markMsg, String time);
    }

    public interface OnNickNameLisenter {
        void OnOkBtn(String msg, PopWindowUtils popWindow);
    }

    public interface OnSelectTypeLisenter {
        void OnOkBtn(int type);
    }
}
