package com.sxyz.rfid.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.google.android.material.tabs.TabLayout;
import com.sxyz.rfid.App;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseActivity;
import com.sxyz.rfid.databinding.ActivityMain2Binding;
import com.sxyz.rfid.ui.my.MyFragment;
import com.sxyz.rfid.ui.out.OutboundFragment;
import com.sxyz.rfid.ui.review.ReviewFragment;
import com.sxyz.rfid.ui.ui.main.TagScanFragmentNew;
import com.sxyz.rfid.utils.L;
import com.ubx.usdk.RFIDSDKManager;
import com.ubx.usdk.USDKManager;
import com.ubx.usdk.rfid.RfidManager;
import com.ubx.usdk.util.SoundTool;

import java.util.List;

/**
 * 主要功能: 首页
 *
 * @Description:
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui.MainActivity
 * @author: chunyuanwu
 * @date: 2025年12月29日 16:40
 * @version: 1.0.0
 */
public class MainActivity2 extends BaseActivity<ActivityMain2Binding> {
    private final int[] imageViewArray = {R.drawable.chuku_icon, R.drawable.pandian_icon, R.drawable.wode_icon};
    private final String[] strings = {"出库", "盘点", "我的"};
    FragmentTransaction transaction;
    FragmentManager manager;
    public boolean RFID_INIT_STATUS = false;//RFID是否初始化成功
    public RfidManager mRfidManager;//RFID实例
    public int readerType = 0;//阅读器类型

    @Override
    public void initParms(Bundle mBundle) {

    }

    @Override
    protected ActivityMain2Binding getViewBinding() {
        return ActivityMain2Binding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        initRfid();
        //角标，如同QQ的红点
//        final TextBadgeItem mBadgeItem = new TextBadgeItem().setBorderWidth(4).setAnimationDuration(200).setBackgroundColorResource(R.color.red).setHideOnSelect(false).setText("0");
//        mBinding.bottombar.setMode(BottomNavigationBar.MODE_DEFAULT);
//        mBinding.bottombar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);//设置Mode和BackgroundStyle
        mBinding.bottombar.addItem(new BottomNavigationItem(imageViewArray[0], strings[0]).setActiveColorResource(R.color.purple_200)).addItem(new BottomNavigationItem(imageViewArray[1], strings[1]).setActiveColorResource(R.color.purple_200)).addItem(new BottomNavigationItem(imageViewArray[2], strings[2]).setActiveColorResource(R.color.purple_200)).setFirstSelectedPosition(0).setBarBackgroundColor(R.color.c_f9f9fc).initialise();
    }


    @Override
    protected void setListener() {
        mBinding.bottombar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                manager = getSupportFragmentManager();
                transaction = manager.beginTransaction();
                switch (position) {
                    case 0:
                        transaction.replace(R.id.framelayout, OutboundFragment.newInstance());
                        break;
                    case 1:
                        transaction.replace(R.id.framelayout, ReviewFragment.newInstance());
                        break;
                    case 2:
                        transaction.replace(R.id.framelayout, MyFragment.newInstance());
                        break;
                }
                transaction.commit();

            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
//设置初始页面
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.framelayout, OutboundFragment.newInstance());
        transaction.commit();
    }

    @Override
    protected void initData() {

    }

    /**
     * 初始化RFID
     */
    private void initRfid() {
        //初始化音效SDK
        SoundTool.getInstance(App.getContext());
        //初始化SDK
        // 在异步回调中拿到RFID实例
        RFIDSDKManager.getInstance().power(true);

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                boolean connect = RFIDSDKManager.getInstance().connect();
                if (connect) {
                    L.i("initRfid()  success.");
                    RFID_INIT_STATUS = true;
                    readerType = RFIDSDKManager.getInstance().getRfidManager().getReaderType();//80为短距，其他为长距
                    mRfidManager = RFIDSDKManager.getInstance().getRfidManager();
                    String firmware = RFIDSDKManager.getInstance().getRfidManager().getFirmwareVersion();
                    L.i("initRfid: GetReaderType() = " + readerType);
                } else {
                    L.i("initRfid  fail.");
                }

            }
        }, 1500);
    }

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

    @Override
    protected void onDestroy() {
        RFID_INIT_STATUS = false;
        USDKManager.getInstance().release();
        if (mRfidManager != null) {
            mRfidManager.disConnect();
            mRfidManager.release();
            L.i("onDestroyView: rfid close");
        }
        SoundTool.getInstance(App.getContext()).release();
        super.onDestroy();
    }

    /**
     * 退出应用  释放资源
     */
    @Override
    public void finish() {
        SoundTool.getInstance(App.getContext()).release();
        USDKManager.getInstance().release();
        super.finish();
    }
}