package com.example.writeepc.base;

import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.writeepc.dialog.TipsDialog;
import com.google.gson.Gson;
import com.handheld.uhfr.UHFRManager;
import com.uhf.api.cls.Reader;

import cn.pda.serialport.Tools;
import okhttp3.MediaType;

public class BaseActivity extends AppCompatActivity {
    public UHFRManager uhfrManager;
    public byte[] accesspwd;
    public short timeOut = 2500;
    public MediaPlayer music;
    public ProgressDialog dialog;
    public TipsDialog tipsDialog;
    public MediaType mediaType = MediaType.parse("text/xml; charset=utf-8");
    public Gson gson;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tipsDialog = new TipsDialog(this);
        gson = new Gson();

    }


    public void initDialog() {
        dialog = new ProgressDialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("请求接口中...");
    }

    public void initUHFRManager() {
        uhfrManager = UHFRManager.getInstance();
        uhfrManager.setGen2session(false);
        uhfrManager.setPower(30, 30);
        accesspwd = Tools.HexString2Bytes("00000000");
        music = MediaPlayer.create(this, com.example.writeepc.R.raw.msg);
        // int[] power = uhfrManager.getPower();
        // Log.e("TAG", "initUHFRManager:" + power[0]);
        // Log.e("TAG", "initUHFRManager:" + power[1]);
    }

    public void initUHFRManager1() {
        uhfrManager = MyApplication.getApplication().getUhfrManager();
        uhfrManager.setGen2session(false);
        uhfrManager.setPower(22, 22);
        accesspwd = Tools.HexString2Bytes("00000000");
        music = MediaPlayer.create(this, com.example.writeepc.R.raw.msg);
        // int[] power = uhfrManager.getPower();
        // Log.e("TAG", "initUHFRManager:" + power[0]);
        // Log.e("TAG", "initUHFRManager:" + power[1]);
    }


    // 1- 读取TID
    public void readTid() {
        byte[] rdata = new byte[12];
        Reader.READER_ERR readerErr = uhfrManager.getTagData(2, 0, 6, rdata, accesspwd, timeOut);
        Log.e("TAG", "readTid:" + readerErr.name());
        if (readerErr == Reader.READER_ERR.MT_OK_ERR) {
            music.start();
            readTidReault(true, Tools.Bytes2HexString(rdata, 12), rdata);
        } else {
            Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
            readTidReault(false, null, null);
        }

    }

    // 1- 读取EPC
    public void readEPC(int lenth) {
        byte[] rdata = new byte[lenth];
        Reader.READER_ERR readerErr = uhfrManager.getTagData(1, 2, lenth / 2, rdata, accesspwd, timeOut);
        Log.e("TAG", "readTid:" + readerErr.name());
        if (readerErr == Reader.READER_ERR.MT_OK_ERR) {
            music.start();
            readTidReault(true, Tools.Bytes2HexString(rdata, lenth), rdata);
        } else {
            Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
            readTidReault(false, null, null);
        }

    }

    protected void readTidReault(boolean readState, String result, byte[] rdata) {

    }

    // @Override
    // protected void onDestroy() {
    //     super.onDestroy();
    //     if (uhfrManager != null) {
    //         uhfrManager.close();
    //         uhfrManager = null;
    //     }
    //
    // }
}
