package com.mq.mylibrary.base;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.mq.mylibrary.CardData;
import com.mq.mylibrary.tool.LogTool;
import com.mq.mylibrary.tool.M10aRFIDTool;
import com.mq.mylibrary.tool.NFCTool;
import com.mq.mylibrary.tool.SPTool;
import com.mq.mylibrary.tool.ToastTool;

/**
 * 创建人 mQ
 * 创建时间 2019/8/6 0006 19:56
 * 文件名称 BaseNfcActivity
 * 说明:  NFC界面基类
 **/
public abstract class BaseNfcActivity extends BaseActivity {

    // NFC适配器
    public NfcAdapter nfcAdapter = null;
    // 传达意图
    public PendingIntent pi = null;
    // 是否支持NFC功能的标签
    public boolean isNFC_support = false;

    public BaseCard mBaseCard;
    private String mRwType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRwType = (String) SPTool.get(BaseConstant.READ_WRITE_TYPE, "");

        PackageManager pm = getPackageManager();
        // 初始化设备支持NFC功能
        isNFC_support = pm.hasSystemFeature(PackageManager.FEATURE_NFC);
        LogTool.d("是否有NFC: " + isNFC_support);

        init();
    }

    public void init() {
        if (mRwType.equals(BaseConstant.R_W_NFC)) {
            // 得到默认nfc适配器
            nfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());
            if (nfcAdapter == null) {
                ToastTool.showLong("请在系统设置中先启用NFC功能！");
                isNFC_support = false;
            }
            if (!nfcAdapter.isEnabled()) {
                ToastTool.showLong("请在系统设置中先启用NFC功能！");
                isNFC_support = false;
            }
            if (isNFC_support) {
                // 初始化PendingIntent，当有NFC设备连接上的时候，就交给当前Activity处理
                pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
                        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            }
            mBaseCard = NFCTool.get(nfcAdapter, newIntent);
        } else if (mRwType.equals(BaseConstant.R_W_M10A)) {
            isNFC_support = false;
            mBaseCard = M10aRFIDTool.get();
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!isNFC_support) {
            // 非nfc
            mBaseCard.open();
        } else {
            // 开始监听NFC设备是否连接，如果连接就发pi意图
            nfcAdapter.enableForegroundDispatch(this, pi,
                    null, null);
            mBaseCard = NFCTool.get(nfcAdapter, newIntent);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isNFC_support) {
            // 当前Activity如果不在手机的最前端，就停止NFC设备连接的监听
            nfcAdapter.disableForegroundDispatch(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBaseCard.close();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            if (!isNFC_support)
                return;
            // 取出封装在intent中的TAG
            newIntent = intent;
            mBaseCard = NFCTool.get(nfcAdapter, newIntent);
        }
        newIntent = intent;
    }

    public Intent newIntent;

    /**
     * 读卡
     */
    public CardData readTag() {
        return mBaseCard.readCard();
    }

    /**
     * 写卡
     */
    public String writeTag(String newCardInfo) {
        return mBaseCard.writeCard(newCardInfo);
    }

    /**
     * 清空IC卡
     */
    public String clearCard() {
        return mBaseCard.clearCard();
    }

}
