package com.phlx.anchorcollect.ui.main;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;

import com.phlx.anchorcollect.App;
import com.phlx.anchorcollect.Configs;
import com.phlx.anchorcollect.data.Repository;
import com.phlx.anchorcollect.db.DbUtil;
import com.phlx.anchorcollect.db.gen.CattleResumeEntityDao;
import com.phlx.anchorcollect.db.gen.DictEntityDao;
import com.phlx.anchorcollect.db.interf.DbQueryCallBack;
import com.phlx.anchorcollect.entity.CattleResumeEntity;
import com.phlx.anchorcollect.entity.DictEntity;
import com.phlx.anchorcollect.entity.GenTableColumn;
import com.phlx.anchorcollect.ui.base.MainBarVM;
import com.phlx.anchorcollect.util.EntityUtils;
import com.uhf.structures.FilterParams;

import org.greenrobot.greendao.query.QueryBuilder;

import java.io.File;
import java.util.List;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class MainVM extends MainBarVM<Repository> {

    public SingleLiveEvent<Integer> onTabClickEvent = new SingleLiveEvent<>();


    public MainVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public void init() {

        Configs.dictList = DbUtil.getInstance().queryAll(DictEntity.class);


    }

    public void initToolbar(String title) {
        setBackTitleText(title);
        setBackRightTextVisible(View.VISIBLE);
        setBackTitleText(title);

    }

    public void setInventoryStatus() {
        Configs.inventoryStatus = SPUtils.getInstance().getBoolean(Configs.SP_INVENTORY_STATUS, false);
        if (!Configs.inventoryStatus) {
            setSpecialInventoryFilter();
        }
    }

    /**
     * 设置为标签最多的盘点
     *
     * @return
     */
    private int setSpecialInventoryFilter() {
        int tsh = 10;
        int maxRepeat = 1;
        int cacheTime = 0;
        int maxMonitorTagInRangeTimeMs = 400;
        int maxMonitorTagOutRangeTimeMs = 200;
        int filterMode = 2;
        maxRepeat |= (filterMode << 14);
        return setInventoryFilter(tsh, maxRepeat, cacheTime, maxMonitorTagInRangeTimeMs, maxMonitorTagOutRangeTimeMs);
    }

    /**
     * 设置盘点过滤门限值
     *
     * @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 BindingCommand OnBaseArchivesClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            onTabClickEvent.setValue(0);
        }
    });

    //性能测定
    public BindingCommand OnSignMeasureClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (Configs.tempUnid == null) {
                ToastUtils.showShort("请先扫描耳标");
                return;
            }
            onTabClickEvent.setValue(1);
        }
    });

    //称重管理
    public BindingCommand OnWeightingManageClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            if(StringUtils.isEmpty(Configs.tempUnid)){
//                ToastUtils.showShort("请先扫描耳标");
//                return;
//            }
            onTabClickEvent.setValue(2);
        }
    });

    //诊疗记录
    public BindingCommand OnMedicalRecordClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (Configs.tempUnid == null) {
                ToastUtils.showShort("请先扫描耳标");
                return;
            }
            onTabClickEvent.setValue(3);
        }
    });

    //免疫记录
    public BindingCommand OnImmunRecordClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            if(StringUtils.isEmpty(Configs.tempUnid)){
//                ToastUtils.showShort("请先扫描耳标");
//                return;
//            }
            onTabClickEvent.setValue(4);
        }
    });

    //配种记录
    public BindingCommand OnBreedingRecordClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (Configs.tempUnid == null) {
                ToastUtils.showShort("请先扫描耳标");
                return;
            }

            QueryBuilder<CattleResumeEntity> builder =
                    DbUtil.getInstance().getQueryBuilder(CattleResumeEntity.class)
                            .where(CattleResumeEntityDao.Properties.Unid.eq(Configs.tempUnid));
            DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<CattleResumeEntity>() {
                @Override
                public void onSuccess(List<CattleResumeEntity> result) {

                    if (result != null && result.size() > 0) {
                        if (result.get(0).getSex().equals("2")) {
                            onTabClickEvent.setValue(5);
                        }else{
                            ToastUtils.showShort("公牛无法配种！");
                        }
                    }

                }

                @Override
                public void onFailed() {
                    ToastUtils.showShort("基础档案数据库查询失败");
                }
            }).queryAsyncAll(CattleResumeEntity.class, builder);
        }
    });

}
