package com.cooleshow.base.presenter;

import com.cooleshow.base.data.net.RetrofitFactory;
import com.cooleshow.base.presenter.view.BaseView;
import com.cooleshow.base.rx.BaseObserver;

import java.lang.ref.WeakReference;

import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class BasePresenter<V extends BaseView> implements IPresenter<V> {

    //    protected V view;
    protected WeakReference<V> viewRef;
    // 管理订阅关系，用于取消订阅
    protected CompositeDisposable compositeDisposable;

    protected AppCompatActivity mContext;

    /**
     * 绑定 View ，一般在初始化时调用
     *
     * @param view
     */
    public void attachView(V view) {
        viewRef = new WeakReference<>(view);
        V v = viewRef.get();
        //        this.view = view;
    }

    /**
     * 解除绑定 View,一般在 onDestroy 中调用
     */
    public void detachView() {
        this.viewRef = null;
        unsubscribe();
    }

    /**
     * 是否绑定了View,一般在发起网络请求之前调用
     *
     * @return
     */
    public boolean isViewAttached() {
        if (viewRef == null) {
            return false;
        }
        return viewRef.get() != null;
    }

    public V getView() {
        return viewRef.get();
    }


    /**
     * 添加订阅
     */
    public void addSubscribe(Observable<?> observable, BaseObserver observer) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        BaseObserver baseObserver = observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer);
        compositeDisposable.add(baseObserver);
    }

    /**
     * 添加订阅,无loading
     */
    public void addSubscribe(Observable<?> observable, DisposableObserver observer) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        DisposableObserver baseObserver = observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer);
        compositeDisposable.add(baseObserver);
    }

    /**
     * 取消订阅
     */
    public void unsubscribe() {
        if (compositeDisposable != null) {
            compositeDisposable.dispose();
        }
        mContext = null;
    }

    protected <T> T create(Class<T> clazz) {
        return RetrofitFactory.Companion.getInstance().create(clazz);
    }

//    protected <T> T createNoToken(Class<T> clazz) {
//        return RetrofitClientNoToken.getInstance().getRetrofit().create(clazz);
//    }
//
//    protected <T> T createUpFile(Class<T> clazz) {
//        return RetrofitClientUpFile.getInstance().getRetrofit().create(clazz);
//    }
}