package com.police.policedatasystem.apply.viewmodel;

import androidx.annotation.NonNull;

import com.police.policedatasystem.apply.fragment.ApplyFragment;
import com.police.policedatasystem.apply.model.ApplyDept;
import com.police.policedatasystem.apply.model.ApplyPerson;
import com.police.policedatasystem.apply.model.ApplyType;
import com.police.policedatasystem.http.CustomCallBack;
import com.police.policedatasystem.indexActivity;
import com.police.policedatasystem.util.UiUtils;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

public class ApplyViewModel {
    private final ApplyFragment fragment;
    public indexActivity activity;

    public ApplyViewModel(ApplyFragment fragment, indexActivity activity) {
        this.fragment = fragment;
        this.activity = activity;
    }

    public void getApplyType() {
        fragment.loadingShow();
        activity.requestClient.getApplyType(new CustomCallBack<List<ApplyType>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<ApplyType> value) {
                value.sort(Comparator.comparing(ApplyType::getDictSort));
                fragment.setPopupWindow(value);
                fragment.loadingNone();
            }
        });
    }

    public void getApplyDept() {
        fragment.loadingShow();
        activity.requestClient.getApplyDept(new CustomCallBack<List<ApplyDept>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<ApplyDept> value) {
                fragment.applyDept = value;
                fragment.loadingNone();
            }
        });
    }

    public void getApplyPerson(String deptId) {
        fragment.loadingShow();
        activity.requestClient.getApplyPerson(deptId,new CustomCallBack<List<ApplyPerson>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<ApplyPerson> value) {
                fragment.applyPeople = value;
                fragment.loadingNone();
            }
        });
    }

    public void getApplyJWLX() {
        fragment.loadingShow();
        activity.requestClient.getApplyJWLX(new CustomCallBack<List<ApplyType>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<ApplyType> value) {
                fragment.applyJWLX = value;
                fragment.loadingNone();
            }
        });
    }

    public void getApplyXXLY() {
        fragment.loadingShow();
        activity.requestClient.getApplyXXLY(new CustomCallBack<List<ApplyType>>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(List<ApplyType> value) {
                fragment.applyXXLY = value;
                fragment.loadingNone();
            }
        });
    }

    public void addApply(HashMap<String, String> map) {
        fragment.loadingShow();
        activity.requestClient.addApply(map, new CustomCallBack<Object>() {
            @Override
            public void onError(@NonNull Call call, @NonNull Exception e) {
                UiUtils.toast(e.getMessage());
                fragment.loadingNone();
            }

            @Override
            public void onSuccess(Object value) {
                fragment.sendSuccess();
                fragment.loadingNone();
            }
        });
    }
}
