package com.sxyz.rfid.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.sxyz.courtai.ui.pojo.TagScan;
import com.sxyz.rfid.App;
import com.sxyz.rfid.R;
import com.sxyz.rfid.ui.ui.main.Activity6BTag;
import com.sxyz.rfid.ui.ui.main.SectionsPagerAdapter;
import com.sxyz.rfid.ui.ui.main.SettingFragment;
import com.sxyz.rfid.ui.ui.main.TagManageFragment;
import com.sxyz.rfid.ui.ui.main.TagScanFragmentNew;
import com.sxyz.rfid.utils.ByteUtils;
import com.ubx.usdk.RFIDSDKManager;
import com.ubx.usdk.USDKManager;
import com.ubx.usdk.rfid.RfidManager;
import com.ubx.usdk.util.QueryMode;
import com.ubx.usdk.util.SoundTool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity1 extends AppCompatActivity {
    public static final String TAG = "usdk";

    public boolean RFID_INIT_STATUS = false;
    public RfidManager mRfidManager;
    public List<String> mDataParents;
    public List<TagScan> tagScanSpinner;
    private List<Fragment> fragments;

    private ViewPager viewPager;
    private TabLayout tabs;
    public int readerType = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        SoundTool.getInstance(App.getContext());
        mDataParents = new ArrayList<>();
        tagScanSpinner = new ArrayList<>();
        initRfid();
        fragments = Arrays.asList(TagScanFragmentNew.newInstance(MainActivity1.this)
                , TagManageFragment.newInstance(MainActivity1.this)
                , SettingFragment.newInstance(MainActivity1.this));
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager(), fragments);
        viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        viewPager.setOffscreenPageLimit(3);
        tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);

    }

    @Override
    protected void onResume() {
        super.onResume();

        String s7 = "1111222233334444555566667777";
        String s8 = "11112222333344445555666677778888";
        String s9 = "111122223333444455556666777788889999";
        String s10 = "1111222233334444555566667777888899990000";
        String s7Llen = ByteUtils.getPC(s7);
        String s8Llen = ByteUtils.getPC(s8);
        String s9Llen = ByteUtils.getPC(s9);
        String s10Llen = ByteUtils.getPC(s10);
        Log.d(TAG, "onResume()  s7Llen:" + s7Llen + "    s8Llen:" + s8Llen + "    s9Llen:" + s9Llen + "    s10Llen:" + s10Llen);
    }


    public void initRfid() {


        // 在异步回调中拿到RFID实例

        RFIDSDKManager.getInstance().power(true);

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {

                boolean connect = RFIDSDKManager.getInstance().connect();

                if (connect) {
                    Log.d(TAG, "initRfid()  success.");
                    RFID_INIT_STATUS = true;
                    readerType = RFIDSDKManager.getInstance().getRfidManager().getReaderType();//80为短距，其他为长距
                    mRfidManager = RFIDSDKManager.getInstance().getRfidManager();
                    String firmware = RFIDSDKManager.getInstance().getRfidManager().getFirmwareVersion();
                    try {
                        ((TagScanFragmentNew) fragments.get(0)).textFirmware.setText(getString(R.string.firmware) + firmware);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.d(TAG, "initRfid: GetReaderType() = " + readerType);
                } else {
                    Log.d(TAG, "initRfid  fail.");
                }

            }
        }, 1500);


    }

//    public void initRfid() {
//        // 在异步回调中拿到RFID实例
//        USDKManager.getInstance().init(new  InitListener() {
//            @Override
//            public void onStatus(boolean status) {
//                if ( status ) {
//                    Log.d(TAG, "initRfid()  success.");
//                    RFID_INIT_STATUS = true;
//                    mRfidManager =   USDKManager.getInstance().getRfidManager();
//                    readerType =  mRfidManager.getReaderType();//80为短距，其他为长距
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            String firmware =   mRfidManager.getFirmwareVersion();
//                            try {
//                                ((TagScanFragment)fragments.get(0)).textFirmware.setText(getString(R.string.firmware) + firmware);
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    });
//                    getScanInteral();
//                    Log.d(TAG, "initRfid: GetReaderType() = " +readerType );
//                }else {
//                    Log.d(TAG, "initRfid  fail.");
//                }
//            }
//        });
//
//    }

    /**
     * 设置查询模式
     *
     * @param mode
     */
    private void setQueryMode(int mode) {
        mRfidManager.setQueryMode(QueryMode.EPC_TID);
    }

    /**
     * 通过TID写标签
     */
    private void writeTagByTid() {
        //写入方法（不需要先选中）
        String tid = "24 length TID";
        String writeData = "need write EPC datas ";
        mRfidManager.writeTagByTid(tid, (byte) 0, (byte) 2, "00000000".getBytes(), writeData);
    }

    @Override
    protected void onDestroy() {

        RFID_INIT_STATUS = false;
        USDKManager.getInstance().release();
        if (mRfidManager != null) {
            mRfidManager.disConnect();
            mRfidManager.release();
            Log.d(TAG, "onDestroyView: rfid close");
        }
        SoundTool.getInstance(App.getContext()).release();
        super.onDestroy();
    }


    @Override
    protected void onPause() {
        SoundTool.getInstance(App.getContext()).release();
        super.onPause();
    }

    /**
     * 设置盘存时间
     *
     * @param interal 0-200 ms
     */
    private void setScanInteral(int interal) {
        int setScanInterval = mRfidManager.setScanInterval(interal);
        Log.v(TAG, "--- setScanInterval()   ----" + setScanInterval);
    }

    /**
     * 获取盘存时间
     */
    private void getScanInteral() {
        int getScanInterval = mRfidManager.getScanInterval();
        Log.v(TAG, "--- getScanInterval()   ----" + getScanInterval);
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);//弹出Menu前调用的方法
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_rate_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_6b:
                Intent intent = new Intent(MainActivity1.this, Activity6BTag.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void finish() {
        Log.e(TAG, " finish: .......");
        SoundTool.getInstance(App.getContext()).release();
        USDKManager.getInstance().release();
        super.finish();
    }

//    @Override
//    public boolean dispatchKeyEvent(KeyEvent event) {
//
//        if((event.getKeyCode() == 520||event.getKeyCode() == 521) &&  event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0){
//            //TODO 按下 扫描键
//            return true;
//        }else  if((event.getKeyCode() == 520||event.getKeyCode() == 521) &&  event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0){
//            //TODO 抬起 扫描键
//            return true;
//        }
//        return super.dispatchKeyEvent(event);
//    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == 523 && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            //TODO 按下 开启盘点

            if (viewPager.getCurrentItem() == 0) {
                TagScanFragmentNew fragment = (TagScanFragmentNew) fragments.get(0);
                fragment.scanStartBtn.callOnClick();
            }
            return true;
        } else if (event.getKeyCode() == 523 && event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
            //TODO 抬起 停止盘点
            return true;
        }
        return super.dispatchKeyEvent(event);
    }


}