package com.phlx.wool.ui.main;

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

import androidx.annotation.NonNull;

import com.phlx.wool.Configs;
import com.phlx.wool.data.Repository;
import com.phlx.wool.db.DbUtil;
import com.phlx.wool.entity.DivisionEntity;
import com.phlx.wool.entity.VillusBuyEntity;
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.PrinterConstants;
import com.printer.sdk.PrinterConstants.Command;
import com.printer.sdk.PrinterInstance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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.SPUtils;

public class MainVM extends MainBarVM<Repository> {

    private List<VillusBuyEntity> villusBuyEntities;

    private static String unitSp;
    public SingleLiveEvent<Boolean> nullEvent = 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() {
            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 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);
        }
    });

}
