package com.tenlionsoft.baselib.core.widget.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.utils.RegexUtils;
import com.tenlionsoft.baselib.utils.TimeUtils;

import androidx.appcompat.widget.SwitchCompat;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 添加汽车使用或所属人员
 */
public class AddCarPersonDialog extends Dialog {

    private View view;
    private Context context;
    private EditText mEtName;
    private EditText mEtIdCard;
    private OnChangeListener mChangeListener;
    private SwitchCompat mSwIsKey;
    private int mIsKey = 0;
    private TextView mTvStartDate;
    private TextView mTvEndDate;
    private EditText mEtPhone;

    private AddCarPersonDialog(Context context) {
        super(context, R.style.dialog_center_alpha);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_add_person_car, null);
        setContentView(view);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        mEtName = view.findViewById(R.id.et_name);
        mEtIdCard = view.findViewById(R.id.et_id_card);
        mEtPhone = view.findViewById(R.id.et_phone);
        mTvStartDate = view.findViewById(R.id.tv_start_date);
        mTvEndDate = view.findViewById(R.id.tv_end_date);
        mSwIsKey = view.findViewById(R.id.sw_is_key);
        mSwIsKey.setOnCheckedChangeListener((buttonView, isChecked) -> mIsKey = isChecked ? 1 : 0);
        mTvStartDate.setOnClickListener(v -> onShowDate(1));
        mTvEndDate.setOnClickListener(v -> onShowDate(2));
        view.findViewById(R.id.tv_cancel).setOnClickListener(v -> this.dismiss());
        view.findViewById(R.id.tv_confirm).setOnClickListener(v -> doConfirm());
        view.findViewById(R.id.tv_both).setOnClickListener(v -> doBoth());
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.dialog_center_alpha);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = (int) (displayMetrics.widthPixels * 0.9);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    private void doBoth() {
        if (checkParams()) {
            if (mChangeListener != null) {
                String name = mEtName.getText().toString().trim();
                String idcard = mEtIdCard.getText().toString().trim();
                String phone = mEtPhone.getText().toString().trim();
                String startDate = mTvStartDate.getText().toString().trim();
                String endDate = mTvEndDate.getText().toString().trim();
                mChangeListener.doChange(name, idcard, phone, startDate, endDate, mIsKey + "", 2);
            }
        }
    }

    private void doConfirm() {
        if (checkParams()) {
            if (mChangeListener != null) {
                String name = mEtName.getText().toString().trim();
                String idcard = mEtIdCard.getText().toString().trim();
                String phone = mEtPhone.getText().toString().trim();
                String startDate = mTvStartDate.getText().toString().trim();
                String endDate = mTvEndDate.getText().toString().trim();
                mChangeListener.doChange(name, idcard, phone, startDate, endDate, mIsKey + "", 1);
            }
        }
    }

    private void onShowDate(int type) {
        TimePickerView mTimePickerView = new TimePickerBuilder(context, (date, v) -> {
            switch (type) {
                case 1:
                    mTvStartDate.setText(TimeUtils.dateToString(date));
                    break;
                case 2:
                    mTvEndDate.setText(TimeUtils.dateToString(date));
                    break;
            }
        })
            .setTitleText("请选日期")
            .setCancelColor(Color.parseColor("#1189FF"))
            .setSubmitColor(Color.parseColor("#1189FF"))
            .isDialog(true)
            .setType(new boolean[]{true, true, true, false, false, false})
            .setTitleColor(Color.parseColor("#1189FF"))
            .build();
        mTimePickerView.show();
    }

    public void HideSoftKeyBoardDialog(Activity activity) {
        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        } catch (Exception ex) {


        }
    }

    public void addOnChangeListener(OnChangeListener listener) {
        this.mChangeListener = listener;
    }

    public interface OnChangeListener {
        void doChange(String name, String idcard, String phone, String startTime, String endTime, String isUse, int type);
    }

    /**
     * 校验参数
     */
    private boolean checkParams() {
        String originPwd = mEtName.getText().toString().trim();
        if (TextUtils.isEmpty(originPwd)) {
            ToastUtils.show("请输入姓名");
            return false;
        }
        String idcard = mEtIdCard.getText().toString().trim();
        if (TextUtils.isEmpty(idcard) || !RegexUtils.isIDNumber(idcard)) {
            ToastUtils.show("请输入正确的身份证号码");
            return false;
        }
        String phone = mEtPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone) || !RegexUtils.isTel(phone)) {
            ToastUtils.show("请输入合法的联系电话");
            return false;
        }
        return true;
    }


    public static class DialogBuilder {
        private Context mContext;

        public DialogBuilder(Context context) {
            this.mContext = context;
        }


        public AddCarPersonDialog build() {
            return new AddCarPersonDialog(mContext);
        }
    }
}