package com.phlx.anchorcollect.ui.fragment.vm;

import android.app.Application;

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

import com.phlx.anchorcollect.BR;
import com.phlx.anchorcollect.Configs;
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.entity.WeightManEntity;
import com.phlx.anchorcollect.ui.fragment.list.CollectItem;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CollectListVM extends BaseViewModel<Repository> {

    //功能
    private String collectType;

    //给RecyclerView添加ObservableList
    public ObservableList<CollectItem> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<CollectItem> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_collect_rv);

    public SingleLiveEvent<Boolean> onItemClickEvent = new SingleLiveEvent<>();

    public ObservableField<String> title1 = new ObservableField<>("可视耳标号");
    public ObservableField<String> title2 = new ObservableField<>("");
    public ObservableField<String> title3 = new ObservableField<>("");
    public ObservableField<String> title4 = new ObservableField<>("");

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

    public void changeTab() {
        onItemClickEvent.call();
    }

    public void setCollectType(String collectType) {
        this.collectType = collectType;
        switch (collectType) {
            case Configs.BASE_ARCHIVES:
                title2.set("品种");
                title3.set("出生难易度");
                title4.set("出生日期");
                break;
            case Configs.SIGN_MEASURE:
                title2.set("事件名称");
                title3.set("圈号");
                title4.set("称重日期");
                break;
            case Configs.TREATMENT_RECORD:
                title2.set("几牙");
                title3.set("体重");
                title4.set("测量日期");
                break;
            case Configs.WEIGHTING_MANAGE:
                title2.set("病因");
                title3.set("用药");
                title4.set("诊疗日期");
                break;
            case Configs.IMMUN_RECORD:
                title2.set("免疫有效期");
                title3.set("疫苗名称");
                title4.set("免疫日期");
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    public void refreshList() {
        switch (collectType) {
            case Configs.BASE_ARCHIVES:
                refreshCard();
                break;
            case Configs.SIGN_MEASURE:
                refreshWeight();
                break;
            case Configs.TREATMENT_RECORD:
                refreshSign();
                break;
            case Configs.WEIGHTING_MANAGE:
                refreshHealth();
                break;
            case Configs.IMMUN_RECORD:
                refreshImmun();
                break;
        }
    }

    private void refreshCard() {
        QueryBuilder<CardEntity> builder =
                DbUtil.getInstance().getQueryBuilder(CardEntity.class);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<CardEntity>() {
            @Override
            public void onSuccess(List<CardEntity> result) {
                observableList.clear();
                for (CardEntity ce : result) {
                    CollectItem ci = new CollectItem(CollectListVM.this, ce.getUnid() + ""
                            , ce.getVarieties(), ce.getEaseBirth(), ce.getBirthdate());
                    observableList.add(ci);
                }
            }

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

    private void refreshWeight() {
        QueryBuilder<WeightManEntity> builder =
                DbUtil.getInstance().getQueryBuilder(WeightManEntity.class);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<WeightManEntity>() {
            @Override
            public void onSuccess(List<WeightManEntity> result) {
                observableList.clear();
                for (WeightManEntity ce : result) {
//                    CollectItem ci = new CollectItem(CollectListVM.this, ce.getUnid() + ""
//                            , ce.getWeightName(), ce.getCircleNo(), ce.getWeightingDate());
//                    observableList.add(ci);
                }
            }

            @Override
            public void onFailed() {
                ToastUtils.showShort("称重管理数据库查询失败");
            }
        }).queryAsyncAll(WeightManEntity.class, builder);
    }

    private void refreshSign() {
        QueryBuilder<PerformanceEntity> builder =
                DbUtil.getInstance().getQueryBuilder(PerformanceEntity.class);
        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<PerformanceEntity>() {
            @Override
            public void onSuccess(List<PerformanceEntity> result) {
                observableList.clear();
                for (PerformanceEntity ce : result) {
                    CollectItem ci = new CollectItem(CollectListVM.this, ce.getUnid() + ""
                            , ce.getAge(), ce.getWeight(), ce.getMeasureDate());
                    observableList.add(ci);
                }
            }

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

    private void refreshHealth() {
//        QueryBuilder<MedicalEntity> builder =
//                DbUtil.getInstance().getQueryBuilder(MedicalEntity.class);
//        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<MedicalEntity>() {
//            @Override
//            public void onSuccess(List<MedicalEntity> result) {
//                observableList.clear();
//                for (MedicalEntity ce : result) {
//                    CollectItem ci = new CollectItem(CollectListVM.this, ce.getUnid() + ""
//                            , ce.getCauseOf(), ce.getDrugName(), ce.getTreatDate());
//                    observableList.add(ci);
//                }
//            }
//
//            @Override
//            public void onFailed() {
//                ToastUtils.showShort("健康管理数据库查询失败");
//            }
//        }).queryAsyncAll(MedicalEntity.class, builder);
    }

    private void refreshImmun() {
//        QueryBuilder<ImmuneEntity> builder =
//                DbUtil.getInstance().getQueryBuilder(ImmuneEntity.class);
//        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<ImmuneEntity>() {
//            @Override
//            public void onSuccess(List<ImmuneEntity> result) {
//                observableList.clear();
//                for (ImmuneEntity ce : result) {
//                    CollectItem ci = new CollectItem(CollectListVM.this, ce.getUnid() + ""
//                            , ce.getVaccineName(), ce.getProductName(), ce.getImmuneDate());
//                    observableList.add(ci);
//                }
//            }
//
//            @Override
//            public void onFailed() {
//                ToastUtils.showShort("免疫管理数据库查询失败");
//            }
//        }).queryAsyncAll(ImmuneEntity.class, builder);
    }
}
