package com.chexiang.myhome.external.vinkeyboard;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.chexiang.myhome.R;
import com.chexiang.myhome.widget.components.VinEditText;

public class KeyBoardPopWindow extends PopupWindow {
    public static final String INPUT_LICENSE_COMPLETE = "com.chexiang.myhome.external.vinkeyboard";
    public static final String INPUT_LICENSE_KEY = "VIN";

    private View mainView;
    private VinKeyboardUtil keyboardUtil;
    private ImageView closeBtn;

    BroadcastReceiver receiver;
    Context mContext;
    EditText editText;
    VinEditText.OnActionListener onActionListener;

    public KeyBoardPopWindow(final Context context, EditText editText, VinEditText.OnActionListener onActionListener) {
        super(context);
        this.mContext = context;
        //窗口布局
        mainView = LayoutInflater.from(context).inflate(R.layout.keyboardlayout_vin, null);
        setContentView(mainView);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        initView();
        this.editText = editText;
        this.onActionListener = onActionListener;
    }

    private void initView() {
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景透明
        setBackgroundDrawable(new ColorDrawable(0));

        closeBtn = (ImageView) mainView.findViewById(R.id.closeBtn);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    /** 注册广播接受来自keyboard的车牌号 */
    public void registerReceiver() {
        IntentFilter finishFilter = new IntentFilter(INPUT_LICENSE_COMPLETE);
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (keyboardUtil != null) {
                    keyboardUtil.hideKeyboard();
                }
                if (onActionListener != null) {
                    onActionListener.onAction();
                }
                KeyBoardPopWindow.this.dismiss();
            }
        };
        mContext.registerReceiver(receiver, finishFilter);
    }

    /** 展示popwindow */
    public void showKeyBoardPopWindow(View parent) {
        if (!this.isShowing()) {
            registerReceiver();
            showKeyBoard();
            this.showAsDropDown(parent, 0, 0);
        } else {
            this.dismiss();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mContext.unregisterReceiver(receiver);
        hideKeyBoard();
    }

    /** 展示popwindow中的键盘布局 */
    private void showKeyBoard() {
        if (keyboardUtil == null) {
            keyboardUtil = new VinKeyboardUtil(mContext, mainView, editText);
        }
        keyboardUtil.showKeyboard();
    }

    /** 隐藏popwindow中的键盘布局 */
    private void hideKeyBoard() {
        keyboardUtil.hideKeyboard();
        keyboardUtil = null;
    }


    /** 通过接口回调将popwindow中获取的车牌号传给Activity界面 */
    public interface LicenseListener {
        void setLicense(String license);
    }
}