package com.phlx.anchorcollect_p.ui.main;

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

import androidx.annotation.NonNull;


import com.phlx.anchorcollect_p.Configs;
import com.phlx.anchorcollect_p.data.Repository;
import com.phlx.anchorcollect_p.db.DbUtil;
import com.phlx.anchorcollect_p.db.gen.CattleResumeEntityDao;
import com.phlx.anchorcollect_p.db.gen.DictEntityDao;
import com.phlx.anchorcollect_p.db.interf.DbQueryCallBack;
import com.phlx.anchorcollect_p.entity.CattleResumeEntity;
import com.phlx.anchorcollect_p.entity.DictEntity;
import com.phlx.anchorcollect_p.entity.FrozenSemenEntity;
import com.phlx.anchorcollect_p.ui.base.MainBarVM;

import org.greenrobot.greendao.query.QueryBuilder;

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.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() {
        QueryBuilder<DictEntity> builder =
                DbUtil.getInstance().getQueryBuilder(DictEntity.class)
                        .orderAsc(DictEntityDao.Properties.DictSort);
        Configs.dictList = DbUtil.getInstance().queryAll(DictEntity.class, builder);
        Configs.frozenSemenList = DbUtil.getInstance().queryAll(FrozenSemenEntity.class);

    }

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

    }


    //基础信息
    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().queryAsyncAll(CattleResumeEntity.class, builder, 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("基础档案数据库查询失败");
                }
            });
        }
    });

}
