package com.casic.dcms.mvp.model.impl;

import com.casic.dcms.bean.AuthorizeOrAdjustBean;
import com.casic.dcms.bean.DelayedOrPostBean;
import com.casic.dcms.bean.PackageOrDeptBean;
import com.casic.dcms.listener.OnGetAuthorizeListener;
import com.casic.dcms.listener.OnGetDelayedListener;
import com.casic.dcms.listener.OnGetPackageCaseListener;
import com.casic.dcms.mvp.model.IDeptCaseModel;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class DeptCaseModelImpl implements IDeptCaseModel {

    private OnGetPackageCaseListener packageCaseListener;
    private OnGetAuthorizeListener authorizeListener;
    private OnGetDelayedListener delayedListener;

    public DeptCaseModelImpl(OnGetPackageCaseListener listener) {
        this.packageCaseListener = listener;
    }

    public DeptCaseModelImpl(OnGetAuthorizeListener listener) {
        this.authorizeListener = listener;
    }

    public DeptCaseModelImpl(OnGetDelayedListener listener) {
        this.delayedListener = listener;
    }

    /**
     * {@link IDeptCaseModel}
     */
    @Override
    public Subscription sendDeptCaseRequest(int offset, String pageTag) {
        Observable<PackageOrDeptBean> observable;
        switch (pageTag) {
            case "todo":
                observable = RetrofitServiceManager.getWorkListResult(offset);
                break;
            case "process":
                observable = RetrofitServiceManager.getProcessListResult(offset);
                break;
            case "handled":
                observable = RetrofitServiceManager.getCompletedListResult(offset);
                break;
            default:
                throw new IllegalArgumentException();
        }
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<PackageOrDeptBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e.getCause() != null) {
                    packageCaseListener.onFailure(e);
                }
            }

            @Override
            public void onNext(PackageOrDeptBean resultBean) {
                if (resultBean != null) {
                    packageCaseListener.onSuccess(resultBean);
                }
            }
        });
    }

    /**
     * {@link IDeptCaseModel}
     */
    @Override
    public Subscription sendAuthorizeOrAdjustRequest(int offset, boolean isAdjust) {
        Observable<AuthorizeOrAdjustBean> observable;
        if (isAdjust) {
            observable = RetrofitServiceManager.getCaseAdjustResult(offset);
        } else {
            observable = RetrofitServiceManager.getCaseAuthorizeResult(offset);
        }
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<AuthorizeOrAdjustBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e.getCause() != null) {
                    authorizeListener.onFailure(e);
                }
            }

            @Override
            public void onNext(AuthorizeOrAdjustBean resultBean) {
                if (resultBean != null) {
                    authorizeListener.onSuccess(resultBean);
                }
            }
        });
    }

    /**
     * {@link IDeptCaseModel}
     */
    @Override
    public Subscription sendDelayedOrPostRequest(int offset, boolean isDelayed) {
        Observable<DelayedOrPostBean> observable;
        if (isDelayed) {
            observable = RetrofitServiceManager.getDelayedListResult(offset);
        } else {
            observable = RetrofitServiceManager.getPostponeListResult(offset);
        }
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<DelayedOrPostBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e.getCause() != null) {
                    delayedListener.onFailure(e);
                }
            }

            @Override
            public void onNext(DelayedOrPostBean resultBean) {
                if (resultBean != null) {
                    delayedListener.onSuccess(resultBean);
                }
            }
        });
    }
}
