package com.zxbw.modulemain.presenter

import android.app.Activity
import android.os.Build
import android.text.TextUtils
import android.view.View
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTSplashAd
import com.zxbw.modulemain.contract.SplashContract
import com.zxhl.cms.AppContext
import com.zxhl.cms.ad.upload.Utils.AdUpload
import com.zxhl.cms.ad.upload.model.NewsEntity
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.AppInEntity
import com.zxhl.cms.net.model.other.IsWeChatLoginEntity
import com.zxhl.cms.net.model.qy.ConvertLinkEntityDetail
import com.zxhl.cms.net.model.uc.UserInfo
import com.zxhl.cms.utils.*
import com.zxhl.cms.widget.CenterDialog
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * Created by gaoleichao on 2019/3/16
 */
class SplashPresenter : SplashContract.Presenter {

    private val mView: SplashContract.View

    private val mActivity: Activity

    constructor(mView: SplashContract.View, mActivity: Activity) {
        this.mView = mView
        this.mActivity = mActivity
        checkPermissions()
    }


    companion object {
        val PERMISSIONS = arrayOf(
//            Constant.Permission.PERMISSION_READ_STORAGE,
            Constant.Permission.PERMISSION_PHONE
//                Constant.Permission.PERMISSION_COARSE_LOCATION,
//                Constant.Permission.PERMISSION_FINE_LOCATION
//            , Constant.Permission.PERMISSION_CAMERA
        )
    }

    private var mDisposablePermissions: Disposable? = null


    fun checkPermissions() {
//        Observable.create(ObservableOnSubscribe<Boolean> { e ->
//            val flag = Utils.needPermissions(*PERMISSIONS)
//            e?.onNext(flag)
//            e?.onComplete()
//        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Observer<Boolean> {
//                override fun onSubscribe(d: Disposable) {
//                    mDisposablePermissions = d
//                }
//
//                override fun onNext(aBoolean: Boolean) {
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mView.getDestroyed()) {
//                        return
//                    }
//                    // 缺少权限时, 进入权限配置页面
//                    if (aBoolean) {
//                        mView.jumpPermissionsPage()
//                    } else {
//                        checkLogin()
//                    }
//                }
//
//                override fun onError(e: Throwable) {
//
//                }
//
//                override fun onComplete() {
//                    if (mDisposablePermissions != null && mDisposablePermissions?.isDisposed == false) {
//                        mDisposablePermissions?.dispose()
//                    }
//                }
//            })
//        checkLoginIsBind()
    }

    override fun checkLogin() {
        val user = SettingPreference.getUserInfoData()
        val token = SettingPreference.getUserToken()
        if (user == null && TextUtils.isEmpty(token)) {



        } else {
            UserDataUtils.updateUserInfo(null)

        }
    }



    private fun checkLoginIsBind() {
        ApiClient.homeApi.isWxLogin().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<IsWeChatLoginEntity>() {
                override fun onSuccess(result: IsWeChatLoginEntity?) {
                    Constant.Switch.isBindLogin = result?.status.equals("1")
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {

                }

            })
//        getAppInfo()
    }

}