package com.phlx.anchorcollect.ui.labelchange.list;

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.gen.CattleResumeEntityDao;
import com.phlx.anchorcollect.db.interf.DbQueryCallBack;
import com.phlx.anchorcollect.entity.CattleResumeEntity;
import com.phlx.anchorcollect.entity.InspectEntity;
import com.phlx.anchorcollect.ui.base.BackBarVM;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

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

public class ExchangeListVM extends BackBarVM<Repository> {

    List<InspectEntity> inspectEntities;

    //给RecyclerView添加ObservableList
    public ObservableList<ExchangeItem> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<ExchangeItem> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_exchange_label);
    public final BindingRecyclerViewAdapter<ExchangeItem> adapter = new BindingRecyclerViewAdapter<>();


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

    //初始化ToolBar
    public void initToolbar(List<InspectEntity> inspectEntities) {
        setBackTitleText(getApplication().getResources().getString(R.string.label_inspect));
        setSettingVisibleObservable(View.GONE);
        setBackRightTextVisible(View.GONE);

        this.inspectEntities = inspectEntities;

        initData();
    }

    private void initData() {

        if (inspectEntities == null || inspectEntities.size() < 1) {
            ToastUtils.showShort("无重复标签牛只");
            finish();
        } else {
            queryState(0);
        }


    }

    private void queryState(int position) {
        QueryBuilder<CattleResumeEntity> builder =
                DbUtil.getInstance().getQueryBuilder(CattleResumeEntity.class)
                        .where(CattleResumeEntityDao.Properties.Unid.eq(inspectEntities.get(position).getUnid()));
        int finalI = position;
        DbUtil.getInstance().queryAsyncAll(CattleResumeEntity.class, builder, new DbQueryCallBack<CattleResumeEntity>() {
            @Override
            public void onSuccess(List<CattleResumeEntity> result) {
                if (result != null && result.size() > 0) {
                    ExchangeItem exchangeItem = new ExchangeItem(ExchangeListVM.this, result.get(0), inspectEntities.get(finalI));
                    observableList.add(exchangeItem);
                    if (finalI >= inspectEntities.size() - 1) {
                        return;
                    }
                    queryState(finalI + 1);
                } else {
                    ToastUtils.showShort("无重复标签牛只信息");
                }
            }

            @Override
            public void onFailed() {
                cn.wandersnail.commons.util.ToastUtils.showShort("查询基础信息失败！");
                dismissDialog();
            }
        });
    }
}
