package com.sxyz.rfid.ui;

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

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.blankj.utilcode.util.SPUtils;
import com.sxyz.rfid.App;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseActivity;
import com.sxyz.rfid.databinding.ActivityMainBinding;
import com.sxyz.rfid.presenter.ILoginPresenter;
import com.sxyz.rfid.presenter.imp.LoginPresenterImpl;
import com.sxyz.rfid.presenter.model.LoginModel;
import com.sxyz.rfid.presenter.model.UserInfoModel;
import com.sxyz.rfid.presenter.view.ILoginView;
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.utils.LocalUtils;
import com.ubx.usdk.RFIDSDKManager;
import com.ubx.usdk.USDKManager;
import com.ubx.usdk.rfid.RfidManager;
import com.ubx.usdk.util.SoundTool;

import sql.com.yz.db.DaoSession;

/**
 * 主要功能: 首页
 *
 * @Description:
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui.MainActivity
 * @author: chunyuanwu
 * @date: 2025年12月29日 16:40
 * @version: 1.0.0
 */
public class MainActivity extends BaseActivity<ActivityMainBinding> implements ILoginView {
    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;//阅读器类型
    private ILoginPresenter mILoginPresenter;
    private DaoSession daoSession;

    @Override
    public void initParms(Bundle mBundle) {

    }

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

    @Override
    protected void initView() {
        initRfid();
        mILoginPresenter = new LoginPresenterImpl(this);
        mILoginPresenter.doGetInfo(this);
        //角标，如同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();

//        Out model = new Out();
//        model.setName("111");
//        daoSession = GreenDaoManager.getInstance().getDaoSession();
//        daoSession.insert(model);
    }


    @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(() -> {
            boolean connect = RFIDSDKManager.getInstance().connect();
            if (connect) {
                RFID_INIT_STATUS = true;
                readerType = RFIDSDKManager.getInstance().getRfidManager().getReaderType();//80为短距，其他为长距
                mRfidManager = RFIDSDKManager.getInstance().getRfidManager();
                mRfidManager.setScanInterval(0);//设置扫描间隔，单位毫秒，默认1000ms
                String firmware = RFIDSDKManager.getInstance().getRfidManager().getFirmwareVersion();
            }
        }, 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();
        }
        SoundTool.getInstance(App.getContext()).release();
        super.onDestroy();
    }

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

    @Override
    public void loginSuccess(LoginModel loginModel) {

    }

    @Override
    public void getUserInfoSuccess(UserInfoModel userInfoModel) {
        if (userInfoModel.getUser() != null) {
            SPUtils.getInstance().put(LocalUtils.USER_ID, userInfoModel.getUser().getUserId());//保存账号
            SPUtils.getInstance().put(LocalUtils.USER_NAME, userInfoModel.getUser().getUserName());//保存账号
            SPUtils.getInstance().put(LocalUtils.NICK_NAME, userInfoModel.getUser().getNickName());//保存账号
        }
    }
}