package com.phlx.wool.ui.sync.vm;

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

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


import com.phlx.wool.Configs;
import com.phlx.wool.data.Repository;
import com.phlx.wool.db.DbUtil;
import com.phlx.wool.db.interf.DbIDUCallBack;
import com.phlx.wool.entity.Harmless;
import com.phlx.wool.entity.HarmlessDetail;
import com.phlx.wool.entity.Marking;
import com.phlx.wool.entity.Quarantine;
import com.phlx.wool.entity.QuarantineDetail;
import com.phlx.wool.entity.VillusBuyEntity;
import com.phlx.wool.entity.VillusGaugeEntity;
import com.phlx.wool.ui.base.BackBarVM;

import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.utils.ToastUtils;

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

    public ObservableField<Boolean> isClearMarking = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isClearHarmless = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isClearQuarantine = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isClearVillusBuy = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isClearVillusGauge = new ObservableField<Boolean>(false);
    public ObservableField<Boolean> isClearAll = new ObservableField<Boolean>(false);
    public ObservableInt clearDataVisibility = new ObservableInt();

    private boolean isMarkingClear, isHarmlessClear, isQuarantineClear, isVillusBuyClear, isVillusGaugeClear;

    public ClearVM(@NonNull Application application, Repository repository) {
        super(application, repository);
        if (Configs.isOnLine) {
            clearDataVisibility.set(View.VISIBLE);
        } else {
            clearDataVisibility.set(View.GONE);
        }
    }

    //初始化ToolBar
    public void initToolbar() {
        setBackTitleText("清理数据");
    }

    public BindingCommand<Boolean> onClearMarkingCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isClearMarking.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onClearHarmlessCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isClearHarmless.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onClearQuarantineCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isClearQuarantine.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onVillusBuyCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isClearVillusBuy.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onVillusGaugeCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isClearVillusGauge.set(isChecked);
                }
            });

    public BindingCommand<Boolean> onClearAllCommand =
            new BindingCommand<>(new BindingConsumer<Boolean>() {
                @Override
                public void call(Boolean isChecked) {
                    isClearAll.set(isChecked);
                    isAllSelected(isChecked);
                }
            });

    private void isAllSelected(boolean isSelected) {
        isClearMarking.set(isSelected);
        isClearHarmless.set(isSelected);
        isClearQuarantine.set(isSelected);
        isClearVillusBuy.set(isSelected);
        isClearVillusGauge.set(isSelected);

    }

    public BindingCommand onClearClickCommand = new BindingCommand(() -> {
        isMarkingClear = false;
        isHarmlessClear = false;
        isQuarantineClear = false;
        isVillusBuyClear = false;
        isVillusGaugeClear = false;
        clearNext();
    });

    private void clearNext() {
        if (isClearMarking.get() && !isMarkingClear) {
            deleteAllMarking();
        } else if (isClearQuarantine.get() && !isHarmlessClear) {
            deleteAllQuarantine();
        } else if (isClearHarmless.get() && !isQuarantineClear) {
            deleteAllHarmless();
        } else if (isClearVillusBuy.get() && !isVillusBuyClear) {
            deleteAllVillusBuy();
        } else if (isClearVillusGauge.get() && !isVillusGaugeClear) {
            deleteAllVillusGauge();
        }
    }

    private void deleteAllMarking() {
        showDialog("正在清除数据，请勿关闭屏幕或退出程序");
        DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
            @Override
            public void onNotification(boolean result) {
                isMarkingClear = true;
                if (result) {
                    dismissDialog();
                }
                clearNext();
            }
        }).deleteAsyncAll(Marking.class);
    }

    private void deleteAllQuarantine() {
        showDialog("正在清除数据，请勿关闭屏幕或退出程序");
        DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
            @Override
            public void onNotification(boolean result) {
                isQuarantineClear = true;
                if (result) {
                    dismissDialog();
                    DbUtil.getInstance().deleteAsyncAll(QuarantineDetail.class);
                }
                clearNext();
            }
        }).deleteAsyncAll(Quarantine.class);
    }

    private void deleteAllHarmless() {
        showDialog("正在清除数据，请勿关闭屏幕或退出程序");
        DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
            @Override
            public void onNotification(boolean result) {
                isHarmlessClear = true;
                if (result) {
                    dismissDialog();
                    DbUtil.getInstance().deleteAsyncAll(HarmlessDetail.class);
                }
                clearNext();
            }
        }).deleteAsyncAll(Harmless.class);
    }

    private void deleteAllVillusBuy() {
        showDialog("正在清除绒毛购买数据，请勿关闭屏幕或退出程序");
        DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
            @Override
            public void onNotification(boolean result) {
                isVillusBuyClear = true;
                if (result) {
                    dismissDialog();
                    DbUtil.getInstance().deleteAsyncAll(VillusBuyEntity.class);
                }
                clearNext();
            }
        }).deleteAsyncAll(VillusBuyEntity.class);
    }

    private void deleteAllVillusGauge() {
        showDialog("正在清除绒毛测量数据，请勿关闭屏幕或退出程序");
        DbUtil.getInstance().setDbIDUCallBack(new DbIDUCallBack() {
            @Override
            public void onNotification(boolean result) {
                isVillusGaugeClear = true;
                if (result) {
                    dismissDialog();
                    DbUtil.getInstance().deleteAsyncAll(VillusGaugeEntity.class);
                }
                clearNext();
            }
        }).deleteAsyncAll(VillusGaugeEntity.class);
    }


}
