package com.sxyz.rfid.ui.out;

import android.bluetooth.le.ScanCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;

import androidx.appcompat.app.AlertDialog;
import androidx.core.widget.PopupWindowCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sxyz.rfid.App;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseActivity;
import com.sxyz.rfid.databinding.ActivityOutboundBinding;
import com.sxyz.rfid.presenter.IOutBoundPresenter;
import com.sxyz.rfid.presenter.imp.OutBoundPresenterImpl;
import com.sxyz.rfid.presenter.model.OutBoundDetailModel;
import com.sxyz.rfid.presenter.model.OutboundFlowingWaterModel;
import com.sxyz.rfid.presenter.model.OutboundModel;
import com.sxyz.rfid.presenter.view.IOutBoundView;
import com.sxyz.rfid.ui.out.adapter.OutBoundDetailsAdapter;
import com.sxyz.rfid.ui.ui.main.TagScanFragmentNew;
import com.sxyz.rfid.utils.ToastUtil;
import com.sxyz.rfid.widgets.OutBoundPopupWindow;
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.List;

/**
 * 主要功能: 出库
 *
 * @Description: 出库
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui.ui.out
 * @author: chunyuanwu
 * @date: 2025年12月27日 16:40
 * @version: 1.0.0
 */
public class OutboundActivity extends BaseActivity<ActivityOutboundBinding> implements IOutBoundView {
    //    private DaoSession daoSession;
    private OutBoundDetailsAdapter mOutBoundAdapter;
    private List<OutBoundDetailModel.RowsBean> list = new ArrayList<>();
    private OutboundModel.RowsBean item;
    private IOutBoundPresenter mPresenter;
    private OutBoundPopupWindow popupWindow;
    private ScanCallback callback;

    @Override
    public void initParms(Bundle mBundle) {
        item = (OutboundModel.RowsBean) mBundle.getSerializable("item");
    }

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

    @Override
    protected void initView() {
        initToolBar("出库");
        mPresenter = new OutBoundPresenterImpl(this);
        mPresenter.doOutBoundDetailList(this, item.getOrderId());
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mOutBoundAdapter = new OutBoundDetailsAdapter(R.layout.item_outbound_details, list);
        mBinding.recyclerView.setAdapter(mOutBoundAdapter);
//        Out model = new Out();
//        model.setName("111");
//        daoSession = GreenDaoManager.getInstance().getDaoSession();
//        daoSession.insert(model);
    }

    @Override
    protected void setListener() {
        mOutBoundAdapter.setOnItemClickListener(new OutBoundDetailsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, OutBoundDetailModel.RowsBean item) {
                IOutBoundPresenter presenter = new OutBoundPresenterImpl(new IOutBoundView() {
                    @Override
                    public void outBoundSuccess(OutboundModel model) {

                    }

                    @Override
                    public void outBoundLoadMoreSuccess(OutboundModel model) {

                    }

                    @Override
                    public void outBoundDetailSuccess(OutBoundDetailModel model) {

                    }

                    @Override
                    public void outBoundFlowingWaterSuccess(OutboundFlowingWaterModel model) {
                        item.setDetailList(model.getRows());
                        mOutBoundAdapter.notifyItemChanged(position);
                    }

                    @Override
                    public void outBoundCancelSuccess() {

                    }

                    @Override
                    public void outBoundDetailCancelSuccess() {

                    }
                });
                presenter.doOutBoundFlowingWaterList(OutboundActivity.this, item.getItemId());
            }

            /**
             * 出库明细-撤销操作
             *
             * @param position 出库明细位置
             * @param item 出库明细
             */
            @Override
            public void onItemCancelClick(int position, OutBoundDetailModel.RowsBean item) {
                //弹窗
                new AlertDialog.Builder(OutboundActivity.this).setTitle("确认撤销吗？").setPositiveButton("确认", (dialog, which) -> {
                    mPresenter.doOutBoundDetailCancel(OutboundActivity.this, item.getItemId());
                }).setNegativeButton("取消", null).show();
            }

            /**
             * 出库流水-撤销操作
             *
             * @param position 出库流水位置
             * @param item 出库流水
             */
            @Override
            public void onItemCancelChildClick(int position, OutboundFlowingWaterModel.RowsBean item) {
                //弹窗
                new AlertDialog.Builder(OutboundActivity.this).setTitle("确认撤销吗？").setPositiveButton("确认", (dialog, which) -> {
                    mPresenter.doOutBoundCancel(OutboundActivity.this, item.getRelationId());
                }).setNegativeButton("取消", null).show();
            }
        });
        //rfid出库
        mBinding.btnRfidOutbound.setOnClickListener(v -> {
            popupWindow = new OutBoundPopupWindow(mActivity);
            popupWindow.show();

//            try {
//                setCallback();
//                RFIDSDKManager.getInstance().getRfidManager().inventorySingle();//单个读取EPC 或tid
////                RFIDSDKManager.getInstance().getRfidManager().startRead();//少量标签盘点建议使用：0；盘点标签超过 100-200建议使用：1.
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        });
        //扫码出库
        mBinding.btnScanOutbound.setOnClickListener(v -> {

        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == 523 && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
//            setCallback();
            //TODO 按下 开启盘点
//            RFIDSDKManager.getInstance().getRfidManager().inventorySingle();//单个读取EPC 或tid
//            RFIDSDKManager.getInstance().getRfidManager().startRead();//少量标签盘点建议使用：0；盘点标签超过 100-200建议使用：1.
            mBinding.btnRfidOutbound.callOnClick();
            return true;
        } else if (event.getKeyCode() == 523 && event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
            //TODO 抬起 停止盘点
            if (RFIDSDKManager.getInstance().getRfidManager() != null) {
                RFIDSDKManager.getInstance().getRfidManager().stopInventory();
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

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

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

    class ScanCallback implements IRfidCallback {
        @Override
        public void onInventoryTag(String EPC, final String TID, final String strRSSI) {
            Log.e("xxx", "onInventoryTag:............... epc:" + EPC + "    tid:" + TID);
            SoundTool.getInstance(App.getContext()).playBeep(1);
        }

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


    @Override
    protected void initData() {

    }

    @Override
    public void outBoundSuccess(OutboundModel model) {

    }

    @Override
    public void outBoundLoadMoreSuccess(OutboundModel model) {

    }

    @Override
    public void outBoundDetailSuccess(OutBoundDetailModel model) {
        list.clear();
        mOutBoundAdapter.setList(model.getRows());
    }

    @Override
    public void outBoundFlowingWaterSuccess(OutboundFlowingWaterModel model) {
    }

    @Override
    public void outBoundCancelSuccess() {
        ToastUtil.showShort("撤销成功");
        mPresenter.doOutBoundDetailList(this, item.getOrderId());
    }

    @Override
    public void outBoundDetailCancelSuccess() {
        ToastUtil.showShort("撤销成功");
        mPresenter.doOutBoundDetailList(this, item.getOrderId());
    }

    @Override
    public void onPause() {
        super.onPause();
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RFIDSDKManager.getInstance().getRfidManager().stopInventory();
        }
    }
}
