package com.phlx.anchorcollect.rfid;

import com.phlx.anchorcollect.App;
import com.phlx.anchorcollect.util.StringUtil;
import com.uhf.structures.FilterParams;
import com.uhf.structures.InventoryParams;
import com.uhf.structures.LowpowerParams;
import com.uhf.structures.SelectCriteria;
import com.uhf.structures.TagGroup;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2019/09/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class InventoryManger {


    private static InventoryManger inventoryManger;

    /**
     * 从给定的上下文中获取 ConnectManger
     *
     * @return InventoryManger 的一个实例
     */
    public static InventoryManger from() {
        if (inventoryManger == null) {
            synchronized (InventoryManger.class) {
                if (inventoryManger == null)
                    inventoryManger = new InventoryManger();
            }
        }
        return inventoryManger;
    }

    /**
     * 唯一的构造函数
     */
    private InventoryManger() {
    }

    /**
     * 下发盘点指令
     *
     * @param mode   盘点模式:0-单次盘点，1-高速盘点，2-低功耗盘点  默认高速盘点
     * @param status 掩码状态 0-禁用，1-启用
     */
    public void StartInventory(int mode, int status) {
        App.getInstance().getLinkage().startInventory(mode, status);
    }

    /**
     * 下发停止盘点指令
     */
    public void StopInventory() {
        App.getInstance().getLinkage().stopInventory();
    }


    public int setInventoryContent(int address, int area, int len) {
        InventoryParams inventoryParams = new InventoryParams();
        inventoryParams.address = address;
        inventoryParams.inventoryArea = area;
        inventoryParams.len = len;
        return App.getInstance().getLinkage().Radio_SetInventoryParams(inventoryParams);
    }

    public InventoryParams getInventoryContent() {
        InventoryParams inventoryParams = new InventoryParams();
        if (App.getInstance().getLinkage().Radio_GetInventoryParams(inventoryParams) == 0) {
            return inventoryParams;
        } else {
            return null;
        }
    }


    /**
     * 设置低功耗参数
     * <p>
     * 盘点模式:UHF 库控制持续盘点: INVENTORY_MODE_USER_CONTINUE (2)下生效，
     *
     * @param highPerformanceTime 维持高性能的时间
     * @param inventoryOnTime     盘点时间
     * @param inventoryOffTime    停止时间
     */
    public int setLowPower(int highPerformanceTime, int inventoryOnTime, int inventoryOffTime) {
        LowpowerParams lowpowerParams = new LowpowerParams();
        lowpowerParams.setHighPerformanceTime(highPerformanceTime);
        lowpowerParams.setInventoryOnTime(inventoryOnTime);
        lowpowerParams.setInventoryOffTime(inventoryOffTime);
        return App.getInstance().getLinkage().setLowpowerScheduler(lowpowerParams);
    }


    /**
     * 从时间(毫秒)中提取出时间(时:分:秒)
     * 时间格式:  时:分
     *
     * @param millisecond 毫秒
     * @return 时间字符串
     */
    public String getTimeFromMillisecond(Long millisecond) {
        String milli;
        long hours = millisecond / (60 * 60 * 1000); //根据时间差来计算小时数
        long minutes = (millisecond - hours * (60 * 60 * 1000)) / (60 * 1000);   //根据时间差来计算分钟数
        long second = (millisecond - hours * (60 * 60 * 1000) - minutes * (60 * 1000)) / 1000;
        //根据时间差来计算秒数
        long milliSecond = millisecond - hours * (60 * 60 * 1000) - minutes * (60 * 1000) -
                second * 1000;   //根据时间差来计算秒数
        if (milliSecond < 100) {
            milli = "0" + milliSecond;
        } else {
            milli = "" + milliSecond;
        }

        return hours + ": " + minutes + ": " + second + ":" + milli;
    }

    /**
     * 设置掩码
     *
     * @return -1 设置失败
     */
    public int setSelectCriteriaMask(int offset, int length, int bank, String maskData) {
        int statue = -1;
        //----------------------------------------------------------------------------第一步 设置设备select值----------------------------------------------------------------------//
        TagGroup tagGroup = new TagGroup();
        if (App.getInstance().getLinkage().Radio_GetQueryTagGroup(tagGroup) != 0) {
            return statue;
        }
        tagGroup.selected = 2;
        if (App.getInstance().getLinkage().Radio_SetQueryTagGroup(tagGroup) != 0) {
            return statue;
        }
        //----------------------------------------------------------------------------第二步 设置设备SelectCriteria----------------------------------------------------------------------//
        SelectCriteria sc = new SelectCriteria();
        if (App.getInstance().getLinkage().get18K6CSelectCriteria(sc) != 0) {
            return statue;
        }
        sc.selectorIdx = 0;
        sc.bank = bank;
        sc.status = 1;
        sc.offset = offset;
        sc.length = length;
        sc.session = 4;
        sc.jq = 0;
        sc.action = 0;
        if (maskData == null) {
            sc.maskData = new byte[64];
        } else {
            sc.maskData = StringUtil.stringToByte(maskData);
        }

        return App.getInstance().getLinkage().set18K6CSelectCriteria(sc);
    }

    public int cancelSelectCriteriaMask() {
        int statue = -1;
        //----------------------------------------------------------------------------第一步 设置设备select值----------------------------------------------------------------------//
        TagGroup tagGroup = new TagGroup();
        if (App.getInstance().getLinkage().Radio_GetQueryTagGroup(tagGroup) != 0) {
            return statue;
        }
        tagGroup.selected = 0;
        if (App.getInstance().getLinkage().Radio_SetQueryTagGroup(tagGroup) != 0) {
            return statue;
        }
        //----------------------------------------------------------------------------第二步 设置设备SelectCriteria----------------------------------------------------------------------//
        SelectCriteria sc = new SelectCriteria();
        if (App.getInstance().getLinkage().get18K6CSelectCriteria(sc) != 0) {
            return statue;
        }
        sc.selectorIdx = 0;
        sc.status = 0;
        sc.session = 0;
        sc.jq = 0;
        sc.action = 0;
        return App.getInstance().getLinkage().set18K6CSelectCriteria(sc);
    }

    /**
     * 设置盘点过滤门限值
     *
     * @param threshold      过滤门限，最大值为 10
     * @param maxRepeatTimes 最大上报次数
     * @param maxCacheTimeMs 最大缓存时间(ms)
     * @return 0--成功 非零--失败
     */
    public int setInventoryFilter(int threshold, int maxRepeatTimes, int maxCacheTimeMs,int maxMonitorTagInRangeTimeMs ,int maxMonitorTagOutRangeTimeMs) {
        FilterParams filterParams = new FilterParams();
        filterParams.setThreshold(threshold);
        filterParams.setFilerFlag(maxRepeatTimes);
        filterParams.setMaxCacheTimeMs(maxCacheTimeMs);
        filterParams.setMaxMonitorTagInRangeTimeMs(maxMonitorTagInRangeTimeMs);
        filterParams.setMaxMonitorTagOutRangeTimeMs(maxMonitorTagOutRangeTimeMs);
        return App.getInstance().getLinkage().setInventoryFilter(filterParams);
    }


    public void resetInventoryFilter(){
         App.getInstance().getLinkage().resetInventoryFilter();
    }

}
