package com.phlx.wool.ui.main;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.phlx.wool.Configs;
import com.phlx.wool.data.Repository;
import com.phlx.wool.entity.Marke;
import com.phlx.wool.entity.VillusBuyEntity;
import com.phlx.wool.params.CattleResponse;
import com.phlx.wool.ui.base.MainBarVM;
import com.phlx.wool.ui.inventory.InventoryActivity;
import com.phlx.wool.ui.setting.SettingActivity;
import com.phlx.wool.ui.sync.activity.ClearActivity;
import com.phlx.wool.ui.sync.activity.DownloadActivity;
import com.phlx.wool.ui.sync.activity.UploadActivity;
import com.phlx.wool.ui.villus.VillusBuyActivity;
import com.phlx.wool.ui.villus.VillusGaugeActivity;
import com.phlx.wool.ui.work.unit.UnitActivity;
import com.phlx.wool.util.PrintLabel;
import com.printer.sdk.PrinterInstance;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
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.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainVM extends MainBarVM<Repository> {

    private List<VillusBuyEntity> villusBuyEntities;

    private static String unitSp;
    public SingleLiveEvent<Boolean> nullEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<String> markCountEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<String> errorEvent = new SingleLiveEvent<>();

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

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

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

    }

    public void initToolbar() {
        setMainTitleText("首页");
    }

    @Override
    protected void mainOnClick() {
        super.mainOnClick();
        drawerEvent.call();
    }


    public BindingCommand OnMarkingClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            unitSp = SPUtils.getInstance().getString(Configs.SP_UNIT_NAME, "");
            if (unitSp.isEmpty()) {
                nullEvent.call();
            } else {
                Bundle mBundle = new Bundle();
                mBundle.putString("type", Configs.WORK_TYPE_M);
                startActivity(InventoryActivity.class, mBundle);
            }
        }
    });

    public BindingCommand OnQuarantineClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            unitSp = SPUtils.getInstance().getString(Configs.SP_UNIT_NAME, "");
            if (unitSp.isEmpty()) {
                nullEvent.call();
            } else {
                Bundle mBundle = new Bundle();
                mBundle.putString("type", Configs.WORK_TYPE_Q);
                startActivity(InventoryActivity.class, mBundle);
            }
        }
    });
    public BindingCommand OnHarmlessClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            unitSp = SPUtils.getInstance().getString(Configs.SP_UNIT_NAME, "");
            if (unitSp.isEmpty()) {
                nullEvent.call();
            } else {
                Bundle mBundle = new Bundle();
                mBundle.putString("type", Configs.WORK_TYPE_H);
                startActivity(InventoryActivity.class, mBundle);
            }
        }
    });


    public BindingCommand OnVillusBuyClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            unitSp = SPUtils.getInstance().getString(Configs.SP_UNIT_NAME, "");
            if (unitSp.isEmpty()) {
                nullEvent.call();
            } else {
                startActivity(VillusBuyActivity.class);
            }
        }
    });

    public BindingCommand OnVillusGaugeClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(VillusGaugeActivity.class);
        }
    });

    public BindingCommand OnVillusPrintClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (Configs.isConnected)
                printEvent.call();
            else {
                Bundle bun = new Bundle();
                bun.putInt("settingStatus", 1);
                startActivity(SettingActivity.class, bun);
            }
        }
    });

    public BindingCommand OnSlaughterEntryClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            unitSp = SPUtils.getInstance().getString(Configs.SP_UNIT_NAME, "");
            if (unitSp.isEmpty()) {
                nullEvent.call();
            } else {
                Bundle mBundle = new Bundle();
                mBundle.putString("type", Configs.WORK_TYPE_SE);
                startActivity(InventoryActivity.class, mBundle);
            }
        }
    });

    public List<String> getVillusBuyList() {
        List<String> villusBuyNoList = new ArrayList<>();
        villusBuyEntities = new ArrayList<>();
        villusBuyEntities = model.getVillusBuy();
        if (villusBuyEntities != null && villusBuyEntities.size() > 0) {
            for (VillusBuyEntity villusBuyEntity : villusBuyEntities) {
                villusBuyNoList.add(villusBuyEntity.getBuyBatch());
            }
        }
        return villusBuyNoList;
    }

    //写打印内容
    public void printVillusBuy(PrinterInstance myPrinter, CharSequence[] text) {

        PrintLabel.printVillusBuy(model, myPrinter, villusBuyEntities, text);

    }

    public BindingCommand OnDownloadClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(DownloadActivity.class);
        }
    });

    public BindingCommand OnUploadClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(UploadActivity.class);
        }
    });

    public BindingCommand OnClearDataClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(ClearActivity.class);
        }
    });

    public BindingCommand OnSelectUnitClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(UnitActivity.class);
        }
    });

    public BindingCommand OnMarkCountClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            unitSp = SPUtils.getInstance().getString(Configs.SP_UNIT_NAME, "");
            if (unitSp.isEmpty()) {
                nullEvent.call();
            } else {
                queryMarkCount();
            }
        }
    });

    //查询牧户打标数量
    private void queryMarkCount() {

        JsonObject jo = new JsonObject();
        jo.addProperty("pastureunitid", SPUtils.getInstance().getString(Configs.SP_UNIT_CODE, ""));

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                SPUtils.getInstance().getString(Configs.SP_UNIT_CODE, ""));
        KLog.e("marke", jo.toString());
        addSubscribe(model.getMarkeInfo(body)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(disposable -> showDialog("正在查询牧户打标数据..."))
                .subscribe((Consumer<CattleResponse<Marke>>) response -> {

                    dismissDialog();
                    if (response.getCode() == 0) {
                        if (response.getData() != null) {
                            String markCountStr = "牧户:" + SPUtils.getInstance().getString(Configs.SP_UNIT_NAME, "")
                                    + "  打标数量：" + response.getData().getMarkecount() + "个!";
                            markCountEvent.setValue(markCountStr);
                        } else {
                            errorEvent.setValue("无此牧户打标数据!!");
                        }

                    } else {
                        errorEvent.setValue("服务器 :" + response.getMsg());
                    }
                }, (Consumer<ResponseThrowable>) throwable -> {
                    dismissDialog();
                    KLog.e(throwable.message);
                    errorEvent.setValue("查询牧户打标信息失败");
                })
        );

    }

}
