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

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;

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

/**
 * Created by Amu on 2017/2/13.
 */

public class ReadNFCDeviceDialog extends AlertDialog {
    @BindView(android.R.id.icon)
    ImageView icon;
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.layout_input1)
    LinearLayout layoutInput1;
    @BindView(R.id.text_hint1)
    TextView textHint1;
    @BindView(R.id.text_hint2)
    TextView textHint2;
    @BindView(R.id.progress_loading)
    ProgressBar progressLoading;
    @BindView(R.id.layout_content_result)
    LinearLayout layoutContentResult;
    @BindView(R.id.text_result_hint)
    TextView textResultHint;
    @BindView(R.id.text_result_rfid)
    TextView textRFID;
    @BindView(android.R.id.button2)
    Button btnCancel;
    @BindView(android.R.id.button1)
    Button btnOK;

    private String strRFID;

    private boolean mbNFCEnabled;

    private ClickListenerInterface mClickListenerInterface;

    public ReadNFCDeviceDialog(Context context, boolean enabled) {
        super(context, R.style.AlertDialog);
        mbNFCEnabled = enabled;
    }

    public interface ClickListenerInterface {
        void doOk(String rfid);
    }

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

    @Subscribe
    public void onMessageEvent(NFCREADMessageEvent messageEvent){
        if (messageEvent.message.equals(NFCREADMessageEvent.NFC_READ_START) ){
           setLoading(true);
        }else if (messageEvent.message.equals(NFCREADMessageEvent.NFC_READ_RESULT)){
            setLoading(false);

            boolean isOk = messageEvent.success;
            String result = messageEvent.result;
            setResult(isOk, result);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_read_nfc_device);
        ButterKnife.bind(this);

        initView();
        initData();
        EventBusUtil.register(this);
    }

    public void init(boolean enabled){
        mbNFCEnabled = enabled;
        initView();
    }

    public void unregisterEvent(){
        EventBusUtil.unregister(this);
    }


    private void initView() {
        alertTitle.setText(R.string.nfc_read_title);
        textHint1.setText(R.string.nfc_title_hint);
        btnOK.setText(R.string.nfc_button_find_device);
        btnCancel.setText(android.R.string.cancel);

        layoutContentResult.setVisibility(View.GONE);
        textHint2.setVisibility(View.VISIBLE);
        if (!mbNFCEnabled){
            textHint2.setText(R.string.nfc_not_enabled_hint);
        }else {
            textHint2.setText(R.string.nfc_content_hint);
        }

        btnOK.setVisibility(View.GONE);
        progressLoading.setVisibility(View.GONE);
    }

    private void initData(){

    }

    private void setLoading(boolean isLoading){
        if (isShowing()){
            textHint2.setVisibility(View.VISIBLE);
            textHint2.setText(R.string.nfc_content_hint);
            layoutContentResult.setVisibility(View.GONE);
            progressLoading.setVisibility(isLoading ? View.VISIBLE : View.GONE);
        }
    }

    private void setResult(boolean ok, String rfid){
        if (isShowing()){
            progressLoading.setVisibility(View.GONE);
            textHint2.setVisibility(View.GONE);
            layoutContentResult.setVisibility(View.VISIBLE);

            if (ok){
                strRFID = rfid;
                textResultHint.setText(R.string.nfc_content_ok_hint);
                textRFID.setText(rfid);
                btnOK.setVisibility(View.VISIBLE);
            }else {
                strRFID = "";
                textResultHint.setText(R.string.nfc_content_faild_hint);
                textRFID.setText(rfid);
                btnOK.setVisibility(View.GONE);
            }
        }
    }

    @OnClick({android.R.id.button1, android.R.id.button2})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                if (mClickListenerInterface != null){
                    if (strRFID != null && !strRFID.isEmpty()){
                        mClickListenerInterface.doOk(strRFID);
                    }
                }
                this.dismiss();
                break;
            case android.R.id.button2:
                this.dismiss();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public static class NFCREADMessageEvent {
        public final static String NFC_READ_START = "nfcReadStart";
        public final static String NFC_READ_RESULT = "nfcReadResult";

        public String message;
        public boolean success;
        public String result;
        public NFCREADMessageEvent(String message, boolean success, String result) {
            this.message = message;
            this.success = success;
            this.result = result;
        }
    }
}
