package com.ifavine.pay.utils;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ifavine.pay.R;
import com.ifavine.pay.adapter.SpinnerAdapter;
import com.ifavine.pay.common.AppContext;
import com.ifavine.pay.view.datepicker.DateUtil;


import java.util.Calendar;
import java.util.Date;

public class DialogMainSetUtils {
    private static Dialog tipDialog;
    private static TextView tipTextView;

    public static final int OK = 0xff888801;
    public static final int CANCEL = 0xff888802;

    public static void dismissTipDialog() {
        if (tipDialog != null) {
            tipDialog.dismiss();
            tipDialog = null;
        }
    }

    public static boolean isShowing() {
        if (tipDialog == null) {
            return false;
        }
        return tipDialog.isShowing();
    }


    /**
     * showConfirmDialog
     */
    public static Dialog showRSTSetDialog(Context mContext, String title, final View.OnClickListener okListener,
                                          View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_rst_num_set, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        Button btn_clear = (Button) view.findViewById(R.id.dialog_clear);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        Spinner spinner1 = (Spinner) view.findViewById(R.id.dialog_sp1);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.dialog_sp2);
        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        if (okListener != null) {
            btn_ok.setOnClickListener(okListener);
        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        String options[] = mContext.getResources().getStringArray(R.array.main_rst_options);
        SpinnerAdapter adapter = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter);

        String options2[] = mContext.getResources().getStringArray(R.array.main_rst_num_options);
        SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);
        dialog.show();
        return dialog;
    }


    /**
     * showConfirmDialog
     *
     * @param imageRes   imageID
     * @param okName     set button name
     * @param cancelName set button name
     */
    public static Dialog showInputDialog(Context mContext, Integer imageRes, String title,
                                         final String msg, String okName, String cancelName, final View.OnClickListener okListener,
                                         View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_common_input_confirm, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_style);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        final EditText tv_msg = (EditText) view.findViewById(R.id.dialog_msg);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        ImageView iv = (ImageView) view.findViewById(R.id.dialog_iv);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        if (msg != null) {
            tv_msg.setHint(msg);
            tv_msg.setVisibility(View.VISIBLE);
        }
        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        btn_ok.setText(okName);
        btn_cancel.setText(cancelName);
        //set image
        if (imageRes != null) {
            iv.setImageResource(imageRes);
        } else {
            iv.setVisibility(View.GONE);
        }
        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        if (okListener != null) {
            btn_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("".equals(tv_msg.getText().toString().trim())) {
                        AppContext.showToast("请输入" + msg);
                        return;
                    }
                    okListener.onClick(tv_msg);
                }
            });
        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }


    public static Dialog showChangePwdDialog(Context mContext, final String title, final View.OnClickListener okListener,
                                             View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_pwd_set, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_style);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view,
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        final EditText et1 = (EditText) view.findViewById(R.id.dialog_et1);
        final EditText et2 = (EditText) view.findViewById(R.id.dialog_et2);
        final EditText et3 = (EditText) view.findViewById(R.id.dialog_et3);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }


        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        if (okListener != null) {

        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }

    public static Dialog showSetAmtDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                          View.OnClickListener cancelListener, boolean cancelable) {
        View view = View.inflate(mContext, R.layout.dialog_amt_set, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 920;
        dialog.getWindow().setAttributes(params);
        Button btn_ok = (Button) view.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) view.findViewById(R.id.dialog_cancel);
        final TextView tv_date1 = (TextView) view.findViewById(R.id.tv_date1);

        //final EditText et1 = (EditText) view.findViewById(R.id.dialog_et1);
        //final EditText et2 = (EditText) view.findViewById(R.id.dialog_et2);
        //final EditText et3 = (EditText) view.findViewById(R.id.dialog_et3);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }


        //set cancel listener
        if (cancelListener != null) {
            btn_cancel.setOnClickListener(cancelListener);
        } else {
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        //set confirm listener
        if (okListener != null) {

        } else {
            btn_ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tv_date1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showTimePickerDialog(mContext, R.style.dialog_style, tv_date1, Calendar.getInstance());
            }
        });
        dialog.show();
        return dialog;
    }

    static int mYear;
    static int mMonth;
    static int mDay;

    /**
     * 本机消费记录
     *
     * @param mContext
     * @param title
     * @param okListener
     * @param cancelable
     * @return
     */
    public static Dialog showReportLocalDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                               boolean cancelable) {

        View view = View.inflate(mContext, R.layout.dialog_report_local, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 1100;
        dialog.getWindow().setAttributes(params);
        Button btn_search = (Button) view.findViewById(R.id.dialog_search);
        Button btn_close = (Button) view.findViewById(R.id.dialog_close);


        final TextView tv_date1 = (TextView) view.findViewById(R.id.tv_date1);
        final TextView tv_date2 = (TextView) view.findViewById(R.id.tv_date2);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        Spinner spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);

        String options[] = mContext.getResources().getStringArray(R.array.main_report_type);
        SpinnerAdapter adapter = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter);

        String options2[] = mContext.getResources().getStringArray(R.array.main_report_record_type);
        SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }

        //set confirm listener
        if (okListener != null) {

        } else {
            btn_search.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        final Calendar startcal = Calendar.getInstance();
        final Calendar c = Calendar.getInstance();
        tv_date1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date1.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date1.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date2.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date2.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        dialog.show();
        return dialog;
    }


    public static Dialog showReportRecordDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                                boolean cancelable) {

        View view = View.inflate(mContext, R.layout.dialog_report_record, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 1100;
        dialog.getWindow().setAttributes(params);
        Button btn_search = (Button) view.findViewById(R.id.dialog_search);
        Button btn_close = (Button) view.findViewById(R.id.dialog_close);


        final TextView tv_date1 = (TextView) view.findViewById(R.id.tv_date1);
        final TextView tv_date2 = (TextView) view.findViewById(R.id.tv_date2);
        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);

        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        Spinner spinner1 = (Spinner) view.findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        Spinner spinner3 = (Spinner) view.findViewById(R.id.spinner3);

        String options[] = mContext.getResources().getStringArray(R.array.main_report_type);
        SpinnerAdapter adapter = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter);

        String options2[] = mContext.getResources().getStringArray(R.array.main_report_record_type);
        SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);

        String options3[] = mContext.getResources().getStringArray(R.array.main_rst_num_options);
        SpinnerAdapter adapter3 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options3);
        spinner3.setAdapter(adapter3);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }

        //set confirm listener
        if (okListener != null) {

        } else {
            btn_search.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        final Calendar startcal = Calendar.getInstance();
        final Calendar c = Calendar.getInstance();
        tv_date1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date1.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date1.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        tv_date2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String date = tv_date2.getText().toString();
                if (!TextUtils.isEmpty(date)) {
                    c.setTimeInMillis(DateUtil.formatDateStr(date, "yyyy-MM-dd").getTime());
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                }
                DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        startcal.set(Calendar.YEAR, year);
                        startcal.set(Calendar.MONTH, month);
                        startcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        long mis = startcal.getTimeInMillis();
                        tv_date2.setText(DateUtil.formatDate(mis, "yyyy-MM-dd"));
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
        });

        dialog.show();
        return dialog;
    }

    public static Dialog showReportSumDialog(final Context mContext, final String title, final View.OnClickListener okListener,
                                             boolean cancelable) {

        View view = View.inflate(mContext, R.layout.dialog_report_sum, null);
        final Dialog dialog = new Dialog(mContext, R.style.dialog_common);// 创建自定义样式dialog
        dialog.setCancelable(cancelable);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(cancelable); // 设置点击屏幕Dialog不消失
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1800;
        params.height = 1100;
        dialog.getWindow().setAttributes(params);
        Button btn_search = (Button) view.findViewById(R.id.dialog_search);
        Button btn_close = (Button) view.findViewById(R.id.dialog_close);


        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        Spinner spinner1 = (Spinner) view.findViewById(R.id.sp_year);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);

        String options[] = mContext.getResources().getStringArray(R.array.main_year);
        SpinnerAdapter adapter = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options);
        spinner1.setAdapter(adapter);

        String options2[] = mContext.getResources().getStringArray(R.array.main_months);
        SpinnerAdapter adapter2 = new SpinnerAdapter(mContext,
                android.R.layout.simple_spinner_item, options2);
        spinner2.setAdapter(adapter2);

        if (title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }

        //set confirm listener
        if (okListener != null) {

        } else {
            btn_search.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });


        dialog.show();
        return dialog;
    }

}
