package com.phlx.wool.ui.work.unit;

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

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


import com.phlx.wool.BR;
import com.phlx.wool.R;
import com.phlx.wool.data.Repository;
import com.phlx.wool.db.DbUtil;
import com.phlx.wool.db.interf.DbQueryCallBack;
import com.phlx.wool.entity.Unit;
import com.phlx.wool.ui.base.BackBarVM;

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;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2020/04/29
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class UnitVM extends BackBarVM<Repository> {


    public SingleLiveEvent<UnitItem> selectItemLiveData = new SingleLiveEvent<UnitItem>();
    public SingleLiveEvent<Boolean> btnLiveData = new SingleLiveEvent<Boolean>();
    //给RecyclerView添加ObservableList
    public ObservableList<UnitItem> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<UnitItem> itemBinding = ItemBinding.of(BR.viewModel,
            R.layout.item_unit_rv);

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

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

    public SingleLiveEvent<Boolean> finishLoadMore = new SingleLiveEvent<>();
    public ObservableField<String> promptStatement = new ObservableField<>();

    public ObservableInt emptyViewVisibility = new ObservableInt(View.GONE);
    public ObservableInt restBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt rVVisibility = new ObservableInt(View.VISIBLE);
    public ObservableInt fixedBtnVisibility = new ObservableInt(View.VISIBLE);


    private int offset = 0;

    public UnitVM(@NonNull Application application, Repository repository) {
        super(application, repository);

    }

    //初始化ToolBar
    public void initToolbar() {
        setSettingVisibleObservable(View.GONE);
        setSearchVisibleObservable(View.VISIBLE);
        setBackTitleText("选择养殖单位");
    }

    //下拉刷新
    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            offset = 0;
            DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<Unit>() {
                @Override
                public void onSuccess(List<Unit> result) {
                    finishRefreshing.call();
                    if (result != null && result.size() > 0) {
                        showRv();
                        observableList.clear();
                        for (Unit unit : result) {
                            UnitItem item = new UnitItem(UnitVM.this, unit);
                            observableList.add(item);
                        }
                        offset++;
                    }
                }

                @Override
                public void onFailed() {
                    finishRefreshing.call();
                }
            }).queryAsyncOffset(Unit.class, offset);

        }
    });

    public void showRv() {
        rVVisibility.set(View.VISIBLE);
        fixedBtnVisibility.set(View.VISIBLE);
        emptyViewVisibility.set(View.GONE);
        restBtnVisibility.set(View.GONE);
    }

    public void showEmpty() {
        rVVisibility.set(View.GONE);
        fixedBtnVisibility.set(View.GONE);
        emptyViewVisibility.set(View.VISIBLE);
        restBtnVisibility.set(View.VISIBLE);

    }

    //上拉加载
    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<Unit>() {
                @Override
                public void onSuccess(List<Unit> result) {
                    if (result != null && result.size() > 0) {
                        for (Unit unit : result) {
                            UnitItem item = new UnitItem(UnitVM.this, unit);
                            observableList.add(item);
                        }
                        offset++;
                    } else {
                        ToastUtils.showShort("没有更多数据了");
                    }
                    finishLoadMore.call();
                }

                @Override
                public void onFailed() {
                    finishLoadMore.call();
                }
            }).queryAsyncOffset(Unit.class, offset);

        }
    });

    public void loadUnit() {

        DbUtil.getInstance().setDbQueryCallBack(new DbQueryCallBack<Unit>() {
            @Override
            public void onSuccess(List<Unit> result) {
                if (result != null && result.size() > 0) {
                    showRv();
                    observableList.clear();
                    for (Unit unit : result) {
                        UnitItem item = new UnitItem(UnitVM.this, unit);
                        observableList.add(item);
                    }
                    offset++;
                }
            }

            @Override
            public void onFailed() {
            }
        }).queryAsyncOffset(Unit.class, offset);
    }

    /**
     * 获取条目下标
     */
    public int getItemPosition(UnitItem unitItem) {
        return observableList.indexOf(unitItem);
    }


    public void setSearchList(List<Unit> units) {
        observableList.clear();
        for (Unit unit : units) {
            UnitItem item = new UnitItem(UnitVM.this, unit);
            observableList.add(item);
        }
    }

    public void setSearchResult(Unit unit) {
        observableList.clear();
        UnitItem item = new UnitItem(UnitVM.this, unit);
        observableList.add(item);
    }

    @Override
    protected void backSearchOnClick() {
        showSearch.call();
    }

    /**
     * 重新加载
     */
    public BindingCommand onRestClickCommand =
            new BindingCommand(new BindingAction() {
                @Override
                public void call() {
                    loadUnit();
                }
            });
    /**
     * 确定
     */
    public BindingCommand onFixedClickCommand =
            new BindingCommand(new BindingAction() {
                @Override
                public void call() {
                    btnLiveData.call();
                }
            });
}
