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 androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;

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 IpAddrModifyDailog extends AlertDialog {
    @BindView(android.R.id.icon)
    ImageView icon;
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.edit_ipaddr)
    EditText editIpAddr;
    @BindView(R.id.edit_httpport)
    EditText editHttpPort;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;
    private Context mContext;
    private String mIpAddr;
    private ClickListenerInterface mClickListenerInterface;

    public IpAddrModifyDailog(Context context, String addr) {
        super(context, R.style.AlertDialog);
        mContext = context;
        mIpAddr = addr;
    }

    public interface ClickListenerInterface {
        public void doOk(String addr);
        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_ipaddr_modify);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        if (!mIpAddr.isEmpty()){
            editIpAddr.setText(mIpAddr);
        }
/*        if (!mPort.isEmpty()){
            editHttpPort.setText(mPort);
        }*/
        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 addr = editIpAddr.getText().toString();
                //String port = editHttpPort.getText().toString();
                if (addr.isEmpty()){
                    ToastUtil.showToast(mContext, R.string.modify_ipaddr_input_empty);
                    return;
                }
                if (mClickListenerInterface != null){
                    mClickListenerInterface.doOk(addr/*, port*/);
                }
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
            case android.R.id.button2:
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
        }
    }
}
