package com.fxy.common;
import com.elvishew.xlog.XLog;
import com.fxy.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

public class CommonDialog extends Dialog implements View.OnClickListener{
    public EditText contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    private TextView tvContentLabel;
    private ImageButton ibClear;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private String typeCode;

    public CommonDialog(Context context) {
        super(context);
        this.mContext = context;
        onCreate(null);
    }

    public CommonDialog(Context context, int themeResId, String typeCode) {
        super(context, themeResId);
        this.mContext = context;
        setLayout();
        setTypeCode(typeCode);
    }

    public CommonDialog(Context context, int themeResId, String typeCode, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.listener = listener;
        setLayout();
        setTypeCode(typeCode);
    }



    public CommonDialog setTitle(String title){
        this.title = title;
        return this;
    }
    public CommonDialog setTitle(String title,String hint){
        this.title = title;
        if (contentTxt!=null){
            titleTxt.setText(title);
            contentTxt.setHint(hint);

        }
        return this;
    }

    public CommonDialog setContentHint(String hint){
        if (contentTxt!=null){
            contentTxt.setText("");
            contentTxt.setHint(hint);
        }
        return this;
    }

    public CommonDialog setPositiveButton(String name){
        this.positiveName = name;
        return this;
    }

    public CommonDialog setNegativeButton(String name){
        this.negativeName = name;
        return this;
    }

    public CommonDialog setContent(String val){
        if (contentTxt!=null){
            contentTxt.setText(val);
        }
        return this;
    }

    public CommonDialog setTypeCode(String typeCode){
        this.typeCode = typeCode;
        if (tvContentLabel!=null){
            if (typeCode.equals("need_customer")){
                tvContentLabel.setText("FX");
                tvContentLabel.setVisibility(View.VISIBLE);
            }else{
                tvContentLabel.setVisibility(View.GONE);
            }
        }

        return this;
    }

    public CommonDialog setInputType(int type){
        if (contentTxt!=null){
            contentTxt.setInputType(type);
        }
        return this;
    }

    /**
     * 获取当前操作类型
     * @return
     */
    public String getTypeCode(){
        return this.typeCode;
    }

    /**
     * 提供尾部操作关闭
     * @param confirm
     */
    public void closeDialog(boolean confirm){
        if(listener != null){
            if (confirm){
                listener.onClickSubmit(CommonDialog.this, true);
            }else {
                listener.onClickCancel(CommonDialog.this, false);
            }
        }
    }


    protected void setLayout(){
        setContentView(R.layout.dialog_normal_layout);
        setCanceledOnTouchOutside(false);
        initView();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void initView(){
        contentTxt = (EditText) findViewById(R.id.et_content);
        titleTxt = (TextView)findViewById(R.id.title);
        submitTxt = (TextView)findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView)findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        tvContentLabel = (TextView) findViewById(R.id.tv_content_label);
        ibClear = (ImageButton)findViewById(R.id.ib_clear);

        // contentTxt.setText(content);
        if(!TextUtils.isEmpty(positiveName)){
            submitTxt.setText(positiveName);
        }

        if(!TextUtils.isEmpty(negativeName)){
            cancelTxt.setText(negativeName);
        }

        if(!TextUtils.isEmpty(title)){
            titleTxt.setText(title);
        }

        if(!TextUtils.isEmpty(content)){
            contentTxt.setText(content);
        }

        contentTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //XLog.e("KeyCode："+event.getKeyCode());
                if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE ||
                        (event.getKeyCode()==KeyEvent.KEYCODE_ENTER   && actionId == KeyEvent.ACTION_DOWN)) {
                    if (!v.getText().toString().trim().isEmpty()) {
                        if(listener != null){
                            listener.onClickSubmit(CommonDialog.this, true);
                        }
                    }

                }
                return true;
            }
        });
        contentTxt.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().isEmpty()){
                    ibClear.setVisibility(View.GONE);
                }else{
                    ibClear.setVisibility(View.VISIBLE);
                }
            }
        });
        ibClear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (contentTxt!=null){
                    contentTxt.setText("");
                    contentTxt.requestFocus();
                }
                ibClear.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                if(listener != null){
                    listener.onClickCancel(this, false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if(listener != null){
                    listener.onClickSubmit(this, true);
                }
                break;
            case R.id.ib_clear:

                break;
        }
    }
    public interface OnCloseListener{
        void onClickCancel(Dialog dialog, boolean confirm);
        void onClickSubmit(Dialog dialog, boolean confirm);
    }

}