package com.phlx.anchorcollect.ui.info;

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

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.phlx.anchorcollect.BR;
import com.phlx.anchorcollect.R;
import com.phlx.anchorcollect.data.Repository;
import com.phlx.anchorcollect.db.DbUtil;
import com.phlx.anchorcollect.db.interf.DbQueryCallBack;
import com.phlx.anchorcollect.entity.CardEntity;
import com.phlx.anchorcollect.entity.PerformanceEntity;
import com.phlx.anchorcollect.ui.base.BackBarVM;

import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CattleVM extends BackBarVM<Repository> {

    //谱系管理的list
    public ObservableList<CattleItem> observablePedigreeList = new ObservableArrayList<>();
    //体征测定的list
    public ObservableList<CattleItem> observableSignsList = new ObservableArrayList<>();
    //健康管理的list
    public ObservableList<CattleItem> observableHealthyList = new ObservableArrayList<>();
    //免疫管理的list
    public ObservableList<CattleItem> observableImmunityList = new ObservableArrayList<>();

    private static String registrationNo;

    //给RecyclerView添加ItemBinding
    public ItemBinding<CattleItem> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_cattle_rv);


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

    }

    public void initToolbar() {
        setBackTitleText("牛信息汇总");
        setBackRightTextVisible(View.GONE);
    }

    public void setTag(String tag) {
        registrationNo = tag;
        refreshCard();
    }


    private void refreshCard() {

        String sql = ("REGISTRATION_NO IN " +
                "(SELECT REGISTRATION_NO FROM CARD_ENTITY WHERE REGISTRATION_NO =" + registrationNo + " )");
        WhereCondition.StringCondition stringCondition = new WhereCondition.StringCondition(sql);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<CardEntity>() {
            @Override
            public void onSuccess(List<CardEntity> result) {
                observablePedigreeList.clear();
                if (result.size() > 0) {
                    CardEntity cardEntity = result.get(0);
                    observablePedigreeList.add(new CattleItem(CattleVM.this, "耳标", cardEntity.getRegistrationNo()));
                    observablePedigreeList.add(new CattleItem(CattleVM.this, "出生", cardEntity.getEaseBirth()));
                    observablePedigreeList.add(new CattleItem(CattleVM.this, "性别", cardEntity.getSex()));
                    observablePedigreeList.add(new CattleItem(CattleVM.this, "品种", cardEntity.getVarieties()));
                    observablePedigreeList.add(new CattleItem(CattleVM.this, "生日", cardEntity.getBirthdate()));
                    observablePedigreeList.add(new CattleItem(CattleVM.this, "胎次", cardEntity.getChildTime()));
                }
                refreshSign();
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("基础档案数据库查询失败");
                refreshSign();
            }
        }).queryAsync(CardEntity.class, stringCondition);
    }


    private void refreshSign() {
         String sql = ("REGISTRATION_NO IN " +
                "(SELECT REGISTRATION_NO FROM PERFORMANCE_ENTITY WHERE REGISTRATION_NO =" + registrationNo + " )");
        WhereCondition.StringCondition stringCondition = new WhereCondition.StringCondition(sql);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<PerformanceEntity>() {
            @Override
            public void onSuccess(List<PerformanceEntity> result) {
                observableSignsList.clear();
                if (result.size() > 0) {
                    PerformanceEntity performanceEntity = result.get(0);
                    observableSignsList.add(new CattleItem(CattleVM.this, "日期", performanceEntity.getMeasureDate()));
                    observableSignsList.add(new CattleItem(CattleVM.this, "年龄", performanceEntity.getAge()));
                    observableSignsList.add(new CattleItem(CattleVM.this, "体高", performanceEntity.getHeight()));
                    observableSignsList.add(new CattleItem(CattleVM.this, "体长", performanceEntity.getLength()));
                    observableSignsList.add(new CattleItem(CattleVM.this, "胸围", performanceEntity.getChestDeep()));
                    observableSignsList.add(new CattleItem(CattleVM.this, "体重", performanceEntity.getWeight()));
                }
                refreshHealth();
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("体征测定数据库查询失败");
                refreshHealth();
            }
        }).queryAsync(PerformanceEntity.class, stringCondition);
    }

    private void refreshHealth() {
//        String sql = ("REGISTRATION_NO IN " +
//                "(SELECT REGISTRATION_NO FROM MEDICAL_ENTITY WHERE REGISTRATION_NO =" + registrationNo + " )");
//        WhereCondition.StringCondition stringCondition = new WhereCondition.StringCondition(sql);
//        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<MedicalEntity>() {
//            @Override
//            public void onSuccess(List<MedicalEntity> result) {
//                observableHealthyList.clear();
//                if (result.size() > 0) {
//                    MedicalEntity medicalEntity = result.get(0);
//                    observableHealthyList.add(new CattleItem(CattleVM.this, "日期", medicalEntity.getTreatDate()));
//                    observableHealthyList.add(new CattleItem(CattleVM.this, "病因", medicalEntity.getCauseOf()));
//                    observableHealthyList.add(new CattleItem(CattleVM.this, "用药", medicalEntity.getDrugName()));
//                    observableHealthyList.add(new CattleItem(CattleVM.this, "方法", medicalEntity.getDrugMethods()));
//                    observableHealthyList.add(new CattleItem(CattleVM.this, "兽医", medicalEntity.getTreatPersonnel()));
//                }
//                refreshImmunity();
//            }
//
//            @Override
//            public void onFailed() {
//                ToastUtils.showShort("健康管理数据库查询失败");
//                refreshImmunity();
//            }
//        }).queryAsync(MedicalEntity.class, stringCondition);
    }

    private void refreshImmunity() {
        String sql = ("REGISTRATION_NO IN " +
                "(SELECT REGISTRATION_NO FROM IMMUNE_ENTITY WHERE REGISTRATION_NO =" + registrationNo + " )");
        WhereCondition.StringCondition stringCondition = new WhereCondition.StringCondition(sql);
//        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<ImmuneEntity>() {
//            @Override
//            public void onSuccess(List<ImmuneEntity> result) {
//                observableImmunityList.clear();
//                if (result.size() > 0) {
//                    ImmuneEntity immuneEntity = result.get(0);
//                    observableImmunityList.add(new CattleItem(CattleVM.this, "日期", immuneEntity.getImmuneDate()));
//                    observableImmunityList.add(new CattleItem(CattleVM.this, "名称", immuneEntity.getImmuneDosage()));
//                    observableImmunityList.add(new CattleItem(CattleVM.this, "批号", immuneEntity.getLotnumber()));
//                    observableImmunityList.add(new CattleItem(CattleVM.this, "兽医", immuneEntity.getImmunePerson()));
//                }
//            }
//
//            @Override
//            public void onFailed() {
//                ToastUtils.showShort("免疫管理数据库查询失败");
//            }
//        }).queryAsync(ImmuneEntity.class, stringCondition);
    }



}
