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

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.widget.DialogTitle;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.util.app.AppUtil;

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

/**
 * Created by zombiexq on 2016/6/13.
 */
public class InputDialog extends Dialog {
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.edit_content_hint)
    TextView textHint;
    @BindView(R.id.edit_content)
    EditText editContent;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;

    private Context mContext;
    private String title;
    private String value;
    private String hint;
    //private int inputType;
    private ClickListenerInterface mClickListenerInterface;

    public interface ClickListenerInterface {
        void doOk(String value);
        void doCancel();
    }

    public InputDialog(Context context, String title, String value, String hint) {
        super(context, R.style.AlertDialog);
        this.mContext = context;
        this.title = title;
        this.value = value;
        this.hint = hint;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_input);
        ButterKnife.bind(this);

        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

            dialogWindow = getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            lp.width = (int) (d.widthPixels * 0.8);// 高度设置为屏幕的0.6
            dialogWindow.setAttributes(lp);
        }

        initView();
    }

    private void initView(){
        if (!title.isEmpty()) {
            alertTitle.setText(title);
        }
        if (!value.isEmpty()) {
            editContent.setText(value);
        }
        if (!hint.isEmpty()) {
            textHint.setText(hint);
        }
        button1.setText(android.R.string.ok);
        button2.setText(android.R.string.cancel);
    }

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

    @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 content = editContent.getText().toString();
                if (content.isEmpty()){
                    ToastUtil.showToast(mContext, R.string.modify_user_input_empty);
                    return;
                }
                if (mClickListenerInterface != null){
                    mClickListenerInterface.doOk(content);
                }
                hideSoftInput();
                this.dismiss();
                break;
            case android.R.id.button2:
                hideSoftInput();
                this.dismiss();
                break;
        }
    }

    private void hideSoftInput() {
        AppUtil.hideSoftInput(this);
    }
}
