package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;

import com.mgq.base.utils.check.CheckUtil;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/13.
 */

public class UserModifyDailog extends AlertDialog {
    @BindView(android.R.id.icon)
    ImageView icon;
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.layout_input1)
    LinearLayout layoutInput1;
    @BindView(R.id.edit_input1)
    EditText editUserName;
    @BindView(R.id.layout_input2)
    LinearLayout layoutInput2;
    @BindView(R.id.edit_input2)
    EditText editUserMobile;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;
    private Context mContext;
    private String mUserName;
    private String mUserPhone;
    private ClickListenerInterface mClickListenerInterface;
    private int input_type = INPUT_BOTH;

    public static final int INPUT_NAME = 0;
    public static final int INPUT_MOBILE = 1;
    public static final int INPUT_BOTH = 2;

    public UserModifyDailog(Context context, String username, String mobile, int input_type) {
        super(context, R.style.AlertDialog);
        mContext = context;
        mUserName = username;
        mUserPhone = mobile;
        if (input_type >= INPUT_NAME && input_type <= INPUT_BOTH) {
            this.input_type = input_type;
        }
    }

    public interface ClickListenerInterface {
        public void doOk(String name, String mobile);
        public void doCancel();
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.mClickListenerInterface = clickListenerInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        setContentView(R.layout.dialog_user_modify);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        alertTitle.setText(R.string.modify_user_title);
        if (input_type == INPUT_BOTH || input_type == INPUT_NAME){
            layoutInput1.setVisibility(View.VISIBLE);
            if (!mUserName.isEmpty()){
                editUserName.setText(mUserName);
            }
        }else {
            layoutInput1.setVisibility(View.GONE);
        }

        if (input_type == INPUT_BOTH || input_type == INPUT_MOBILE) {
            layoutInput2.setVisibility(View.VISIBLE);
            if ((!mUserPhone.isEmpty())){
                editUserMobile.setText(mUserPhone);
            }
        }else {
            layoutInput2.setVisibility(View.GONE);
        }

        button1.setText(android.R.string.ok);
        button2.setText(android.R.string.cancel);
    }

    @OnClick({android.R.id.button1, android.R.id.button2})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                String name = editUserName.getText().toString();
                if (input_type == INPUT_BOTH || input_type == INPUT_NAME) {
                    if (name.isEmpty()){
                        ToastUtil.showToast(mContext, R.string.modify_user_input_empty);
                        return;
                    }
                }

                String mobile = editUserMobile.getText().toString();
                if (input_type == INPUT_BOTH || input_type == INPUT_MOBILE) {
                    if (mobile.isEmpty()){
                        ToastUtil.showToast(mContext, R.string.modify_user_input_empty);
                        return;
                    }

                    if (!CheckUtil.isPhoneNum(mobile)) {
                        ToastUtil.showToast(mContext, R.string.write_right_phone_number);
                        return;
                    }
                }

                if (mClickListenerInterface != null){
                    mClickListenerInterface.doOk(name, mobile);
                }
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
            case android.R.id.button2:
                if (mClickListenerInterface != null){
                    mClickListenerInterface.doCancel();
                }
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
        }
    }
}
