package cn.muchinfo.rma.view.base

import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.BaseUrlData
import cn.muchinfo.rma.lifecycle.addObserver
import cn.muchinfo.rma.netManage.base.ResponseCallback
import cn.muchinfo.rma.netManage.utils.MyOkHttpUtils
import cn.muchinfo.rma.view.MainActivity
import cn.muchinfo.rma.view.autoWidget.autoSize
import cn.muchinfo.rma.view.base.app.BaseResult
import cn.muchinfo.rma.view.base.app.Constant
import cn.muchinfo.rma.view.base.login.LoginActivity
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import okhttp3.Call
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.imageView
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.relativeLayout
import java.lang.Exception
import java.util.concurrent.TimeUnit

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        QMUIStatusBarHelper.translucent(this)
        getBaseURl()
        relativeLayout {

            imageView {
                scaleType = ImageView.ScaleType.CENTER_CROP
                imageResource = R.mipmap.img_start
            }.lparams(matchParent, matchParent)

            Observable.timer(1000, TimeUnit.MILLISECONDS)//在此页面停留1秒钟跳转到mainactivity
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { goLoginPage() }
                .let { disposable ->
                    addObserver { onDestroy { if (disposable.isDisposed.not()) disposable.dispose() } }
                }
        }
    }

    fun goHomePage() {
        val intent = Intent()
        intent.setClass(this, MainActivity::class.java)
        ActivityUtils.startActivity(intent)
        finish()
    }

    fun goLoginPage() {
        val intent = Intent()
        intent.setClass(this, LoginActivity::class.java)
        ActivityUtils.startActivity(intent)
        finish()
    }

    private fun getBaseURl(){

        MyOkHttpUtils().queryGetForUrl(URL = Constant.baseurl,
            callback = object : ResponseCallback<BaseResult<BaseUrlData>>() {
                override fun onResponse(response: BaseResult<BaseUrlData>?, id: Int) {
                    LogUtils.eTag("response",response?.data?.commSearchUrl)
                    //对rul进行SP存储
                    val data = response?.data
                    SPUtils.getInstance().put(Constant.goCommonSearchUrl,data?.goCommonSearchUrl)
//                    SPUtils.getInstance().put(Constant.goCommonSearchUrl, "http://192.168.30.172:8080/api")
                    SPUtils.getInstance().put(Constant.commSearchUrl,data?.commSearchUrl)
                    SPUtils.getInstance().put(Constant.quoteHost,data?.quoteHost)
                    SPUtils.getInstance().put(Constant.quotePort,data?.quotePort)
                    SPUtils.getInstance().put(Constant.tradeHost,data?.tradeHost)
                    SPUtils.getInstance().put(Constant.tradePort,data?.tradePort)
                    SPUtils.getInstance().put(Constant.uploadUrl,data?.uploadUrl)
                    SPUtils.getInstance().put(Constant.openApiUrl,data?.openApiUrl)

                    // 外网调试环境 swagger: http://218.17.158.45:21003/swagger/index.html

//                    SPUtils.getInstance().put(Constant.goCommonSearchUrl, "http://218.17.158.45:21003/api")
//                    SPUtils.getInstance().put(Constant.commSearchUrl,"http://218.17.158.45:21004/mtp2-search")
//                    SPUtils.getInstance().put(Constant.quoteHost,"218.17.158.45")
//                    SPUtils.getInstance().put(Constant.quotePort,"21002")
//                    SPUtils.getInstance().put(Constant.tradeHost,"218.17.158.45")
//                    SPUtils.getInstance().put(Constant.tradePort,"21001")
//                    SPUtils.getInstance().put(Constant.uploadUrl,"http://218.17.158.45:21007/mtp2-onlineopen/upload")
//                    SPUtils.getInstance().put(Constant.openApiUrl,"http://218.17.158.45:21007/mtp2-onlineopen")
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    LogUtils.eTag("errorbaseurl",e?.message)
                }

            },params = mutableMapOf())
    }

}