package com.sunzee.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sunzee.R;
import com.sunzee.utils.WifiHelper;

/**
 * @author whw
 * @time 2019/3/13
 * @Description 连接wifi
 */
public class WifiLinkDialog extends Dialog implements View.OnClickListener {


    private String text_nameString = null;

    private String capabilities;

    private Context mContext;
    private Button mBtnApplay;
    private TextView mTvTitle;
    private EditText mEtServerId;


    public void setListener(DialogClickListener listener) {
        this.listener = listener;
    }

    private DialogClickListener listener;

    public WifiLinkDialog(@NonNull Context context) {
        super(context, R.style.DialogBgD);//加载样式
        mContext = context;
        setContentView(R.layout.dialog_et);
        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnApplay.setOnClickListener(this);
    }

    protected void initView() {
        mBtnApplay = findViewById(R.id.btn_apply);
        mBtnApplay = findViewById(R.id.btn_apply);
        mTvTitle = findViewById(R.id.tv_title);
        mEtServerId = findViewById(R.id.et_server_id);
    }

    public void setTitle(String text){
        text_nameString=text;
        mTvTitle.setText(text_nameString);
        mEtServerId.setHint(R.string.hint_pas);
        mBtnApplay.setText(R.string.ljlj);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_apply:
                String pwd = mEtServerId.getText().toString();
                WifiHelper wifiHelper = new WifiHelper(getContext());
                wifiHelper.connectWifi(text_nameString, pwd, capabilities);
                hintKeyBoard();
                dismiss();
                break;
        }

    }

    public void setCapabilities(String capabilities) {
        this.capabilities = capabilities;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }


    public void hintKeyBoard() {
        //拿到InputMethodManager
        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        //如果window上view获取焦点 && view不为空
        if(imm.isActive()&&getCurrentFocus()!=null) {
            //拿到view的token 不为空
            if (getCurrentFocus().getWindowToken() != null) {
                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    public interface DialogClickListener {

        void onClickListener(int type,String text);
    }

}
