package com.sxyz.rfid.ui.ui.main;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sxyz.rfid.App;
import com.sxyz.rfid.R;
import com.sxyz.rfid.ui.MainActivity;
import com.sxyz.rfid.ui.MainActivity1;
import com.sxyz.rfid.utils.ByteUtils;
import com.ubx.usdk.RFIDSDKManager;
import com.ubx.usdk.rfid.aidl.IRfidCallback;
import com.ubx.usdk.util.SoundTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TagScanFragmentNew#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TagScanFragmentNew extends Fragment {

    public static final String TAG = "usdk-" + TagScanFragmentNew.class.getSimpleName();
    private List<com.sxyz.courtai.ui.pojo.TagScan> data;
    private HashMap<String, com.sxyz.courtai.ui.pojo.TagScan> mapData;
    private ScanCallback callback;
    private ScanListAdapterRv scanListAdapterRv;
    private static MainActivity1 mActivity;
    private int tagTotal = 0;
    private int errorCount = 0;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ScanFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TagScanFragmentNew newInstance(MainActivity1 activity) {
        mActivity = activity;
        TagScanFragmentNew fragment = new TagScanFragmentNew();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tag_scan, container, false);
    }

    public Button scanStartBtn, clearBtn;
    private RecyclerView scanListRv;
    private TextView scanCountText, scanTotalText;
    public TextView textFirmware;
    private Spinner spinnerMode;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        spinnerMode = view.findViewById(R.id.spinner_inventory_mode);
        scanStartBtn = view.findViewById(R.id.scan_start_btn);
        clearBtn = view.findViewById(R.id.btn_clear);
        scanListRv = view.findViewById(R.id.scan_list_rv);
        scanCountText = view.findViewById(R.id.scan_count_text);
        scanTotalText = view.findViewById(R.id.scan_total_text);
        textFirmware = view.findViewById(R.id.text_firmware);


        scanStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mActivity.RFID_INIT_STATUS) {

                    if (scanStartBtn.getText().equals(getString(R.string.btInventory))) {
                        setCallback();
                        scanStartBtn.setText(getString(R.string.btn_stop_Inventory));
                        setScanStatus(true);
                    } else {
                        scanStartBtn.setText(getString(R.string.btInventory));
                        setScanStatus(false);
                    }
                } else {
                    Log.d(TAG, "scanStartBtn  RFID未初始化 ");
                    Toast.makeText(getActivity(), "RFID Not initialized", Toast.LENGTH_SHORT).show();
                }

            }
        });

        clearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                tagTotal = 0;
                if (mapData != null) {
                    mapData.clear();
                }
                if (mActivity.mDataParents != null) {
                    mActivity.mDataParents.clear();
                }
                if (mActivity.tagScanSpinner != null) {
                    mActivity.tagScanSpinner.clear();
                }
                if (data != null) {
                    data.clear();
                    scanListAdapterRv.setData(data);
                }

                showView();


            }
        });

        mapData = new HashMap<>();
        spinnerMode.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
                            RFIDSDKManager.getInstance().getRfidManager().setQueryMode(position);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                }
        );


        scanListRv.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        scanListRv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        scanListAdapterRv = new ScanListAdapterRv(null, getActivity());
        scanListRv.setAdapter(scanListAdapterRv);

    }

    private final int MSG_UPDATE_UI = 0;
    private final int MSG_STOP_INVENTORY = 1;

    private final int MSG_UPDATE_SINGLE_INV = 2;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_UPDATE_UI:
                    scanListAdapterRv.notifyDataSetChanged();
                    handlerUpdateUI();
                    break;
                case MSG_STOP_INVENTORY:
                    scanListAdapterRv.notifyDataSetChanged();
                    mHandler.removeCallbacksAndMessages(null);
                    if (scanStartBtn.getText().equals(mActivity.getString(R.string.btn_stop_Inventory))) {
                        scanStartBtn.setText(getContext().getString(R.string.btInventory));
                    }
                    break;

                case MSG_UPDATE_SINGLE_INV:
                    RFIDSDKManager.getInstance().getRfidManager().inventorySingle();
                    mHandler.sendEmptyMessageDelayed(MSG_UPDATE_SINGLE_INV, 200);
                    break;
            }

        }
    };


    @Override
    public void onStart() {
        super.onStart();


    }

    private void setScanStatus(boolean isScan) {

        if (isScan) {
            Log.v(TAG, "--- startInventory()   ----");
            handlerUpdateUI();
            try {
                RFIDSDKManager.getInstance().getRfidManager().startRead();//少量标签盘点建议使用：0；盘点标签超过 100-200建议使用：1.
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            Log.v(TAG, "--- stopInventory()   ----");
            if (RFIDSDKManager.getInstance().getRfidManager() != null) {
                RFIDSDKManager.getInstance().getRfidManager().stopInventory();
            }
            handlerStopUI();
        }

    }

    /**
     * 控制A/B面读取
     */
    private void myScan() {

//               int ret = RFIDSDKManager.getInstance().getRfidManager().inventoryOnce((byte)1,(byte)4,(byte)0,(byte)6,(byte)0,(byte)50);//自定义扫描
//                Log.v(TAG, " inventoryOnce()   ret == "+ret);

        byte[] readAdr = new byte[]{0x00, 0x00};//固定2字节,第一个字节是高位，第2字节是低位
        int ret = RFIDSDKManager.getInstance().getRfidManager().inventoryOnceMix((byte) 1, (byte) 4, (byte) 0, (byte) 50, (byte) 2, readAdr, (byte) 6, ByteUtils.hexStringToBytes("00000000"));//自定义扫描
        Log.v(TAG, " InventoryOnceMix()   ret == " + ret);

    }

    private void handlerUpdateUI() {
        if (mHandler != null) {
            mHandler.sendEmptyMessageDelayed(MSG_UPDATE_UI, 500);
        }
    }

    private void handlerStopUI() {
        if (mHandler != null) {
            mHandler.sendEmptyMessageDelayed(MSG_STOP_INVENTORY, 200);
        }
    }


    private long time = 0l;

    /**
     * 单个读取EPC 或tid
     */
    private void inventorySingle() {
        RFIDSDKManager.getInstance().getRfidManager().inventorySingle();
        if (mHandler != null) {
            mHandler.sendEmptyMessageDelayed(MSG_STOP_INVENTORY, 10);
        }
    }


    /**
     * 设置掩码（标签过滤盘存）
     */
    private void setTagMask() {
        RFIDSDKManager.getInstance().getRfidManager().addMask(2, 24, 16, "7020");
    }


    /**
     * 通过TID写入标签数据
     *
     * @param TID     选中的TID
     * @param Mem     标签区域：0-密码区，前2个字是销毁密码，后2个字是访问密码      1-EPC区   2-TID区    3-用户区
     * @param WordPtr 写入的起始字地址
     * @param pwd     密码
     * @param datas   待写入数据
     */
    private void writeTagByTid(String TID, byte Mem, byte WordPtr, byte[] pwd, String datas) {
//                String TID = "E280110C20007642903D094D";
//                byte[] pwd = hexStringToBytes("00000000");
//                String datas = "1111111111111111";
        int ret = RFIDSDKManager.getInstance().getRfidManager().writeTagByTid(TID, (byte) 1, (byte) 2, pwd, datas);
        if (ret == -6) {
            Toast.makeText(mActivity, getContext().getString(R.string.gj_no_support), Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 随机对一张标签写入EPC
     *
     * @param epc      待写入的EPC值 16进制字符串
     * @param password 标签访问密码
     */
    private void writeEpcString(String epc, String password) {
        RFIDSDKManager.getInstance().getRfidManager().writeEpcString(epc, password);
    }


    class ScanCallback implements IRfidCallback {
        @Override
        public void onInventoryTag(String EPC, final String TID, final String strRSSI) {
            Log.e(TAG, "onInventoryTag:............... epc:" + EPC + "    tid:" + TID);
            notiyDatas(EPC, TID, strRSSI);


        }

        /**
         * 盘存结束回调(Inventory Command Operate End)
         */
        @Override
        public void onInventoryTagEnd() {
            Log.d(TAG, "onInventoryTagEnd()");

        }
    }

    int num = 0;

    private void notiyDatas(String s2, String TID, final String strRSSI) {
        final String mapContainStrFinal = s2 + TID;
        Log.d(TAG, "onInventoryTag: EPC: " + s2);

        SoundTool.getInstance(App.getContext()).playBeep(1);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                {
                    if (mapData.containsKey(mapContainStrFinal)) {
                        com.sxyz.courtai.ui.pojo.TagScan tagScan = mapData.get(mapContainStrFinal);
                        tagScan.setCount(mapData.get(mapContainStrFinal).getCount() + 1);
//                    tagScan.setTid(TID);
                        tagScan.setRssi(strRSSI);
                        mapData.put(mapContainStrFinal, tagScan);
                    } else {
                        mActivity.mDataParents.add(s2);

                        com.sxyz.courtai.ui.pojo.TagScan tagScan = new com.sxyz.courtai.ui.pojo.TagScan(s2, TID, strRSSI, 1);
                        mapData.put(mapContainStrFinal, tagScan);
                        mActivity.tagScanSpinner.add(tagScan);
                    }

                    tagTotal++;
                    data = new ArrayList<>(mapData.values());
//                        Log.d(TAG, "onInventoryTag: data = " + Arrays.toString(data.toArray()));

                    showView();
                    /*long nowTime = System.currentTimeMillis();
                    if ((nowTime - time)>500){
                        time = nowTime;
                        data = new ArrayList<>(mapData.values());
//                        Log.d(TAG, "onInventoryTag: data = " + Arrays.toString(data.toArray()));
                        scanListAdapterRv.setData(data);
                    }*/


                }
            }
        });
    }

    private void showView() {
        scanTotalText.setText(tagTotal + "");
        scanListAdapterRv.setData(data);
        scanCountText.setText(mapData.keySet().size() + "");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ........");

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            setCallback();
        } else {
            if (RFIDSDKManager.getInstance().getRfidManager() != null) {
                scanStartBtn.setText(getContext().getString(R.string.btInventory));
                setScanStatus(false);
            }
//            Log.e(TAG, "setUserVisibleHint: 扫描释放声音资源..........." );
//            SoundTool.getInstance(BaseApplication.getContext()).release();

        }

    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: ........");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            setScanStatus(false);
        }

    }


    public void setCallback() {
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {

            if (callback == null) {
                callback = new ScanCallback();
            }
            RFIDSDKManager.getInstance().getRfidManager().registerCallback(callback);
        }
    }

//-----------------------------------------

}