package com.uiuipad.appstore.fragment.featured;

import android.content.Context;
import android.util.Log;

import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.FragmentEvent;
import com.uiuipad.appstore.base.BaseResponse;
import com.uiuipad.appstore.bean.AdminAppInfo;
import com.uiuipad.appstore.bean.AppClassBean;
import com.uiuipad.appstore.bean.AppInfo;
import com.uiuipad.appstore.network.NetInterfaceManager;

import java.util.List;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.subjects.BehaviorSubject;

public class FeaturedPresenter implements FeaturedContact.Presenter {
    private static final String TAG = FeaturedPresenter.class.getSimpleName();

    private Context mContext;
    private FeaturedContact.FeaturedView mView;

    public FeaturedPresenter(Context context) {
        this.mContext = context;
    }

    private BehaviorSubject<FragmentEvent> lifecycle;

    void setLifecycle(BehaviorSubject<FragmentEvent> lifecycle) {
        this.lifecycle = lifecycle;
    }

    public BehaviorSubject<FragmentEvent> getLifecycle() {
        return lifecycle;
    }

    @Override
    public void attachView(@NonNull FeaturedContact.FeaturedView view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }

    @Override
    public void getAdminApp() {
        NetInterfaceManager.getInstance().getAdminAppObservable()
                .compose(RxLifecycle.bindUntilEvent(lifecycle, FragmentEvent.DESTROY))
                .subscribe(new Observer<BaseResponse<List<AdminAppInfo>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAdminApp", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<List<AdminAppInfo>> listBaseResponse) {
                        Log.e("getAdminApp", "onNext: " + listBaseResponse);
                        if (listBaseResponse.code == 200) {
                            List<AdminAppInfo> appInfoList = listBaseResponse.data;
                            mView.setAdminApp(appInfoList);
                        } else {
                            mView.setAdminApp(null);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAdminApp", "onError: " + e.getMessage());
                        mView.getAdminAppError();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAdminApp", "onComplete: ");
                        mView.getAdminAppFinish();
                    }
                });
    }

    @Override
    public void getAdminAppClass() {
        NetInterfaceManager.getInstance().getAdminAppClassObservable()
                .compose(RxLifecycle.bindUntilEvent(lifecycle, FragmentEvent.DESTROY))
                .subscribe(new Observer<BaseResponse<List<AppClassBean>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAdminAppClass", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull BaseResponse<List<AppClassBean>> listBaseResponse) {
                        Log.e("getAdminAppClass", "onNext: " + listBaseResponse);
                        if (listBaseResponse.code == 200) {
                            List<AppClassBean> appClassBeanList = listBaseResponse.data;
                            mView.setAdminAppClass(appClassBeanList);
                        } else {
                            mView.setAdminAppClass(null);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAdminAppClass", "onError: " + e.getMessage());
                        mView.getAdminAppClassError();
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAdminAppClass", "onComplete: ");
                        mView.getAdminAppClassFinish();
                    }
                });
    }
}
