package cn.yyxx.eyuancomm.impl

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import cn.yyxx.eyuancomm.base.entity.SdkChargeInfo
import cn.yyxx.eyuancomm.base.entity.SdkRoleInfo
import cn.yyxx.eyuancomm.base.internal.ICallback
import cn.yyxx.eyuancomm.base.internal.feature.*
import cn.yyxx.eyuancomm.base.utils.Logger
import cn.yyxx.eyuancomm.base.utils.ParamsUtils
import cn.yyxx.eyuancomm.comm.entity.SdkLoginInfo
import cn.yyxx.eyuancomm.comm.network.SdkRequest
import cn.yyxx.eyuancomm.impl.entity.ChannelId
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2022/01/17
 */
class ImplSdkProxy(context: Context) {

    private var mImpl: IFeature? = null
    private var channelId = ""

    init {
        //TODO 配置文件获取渠道id
        channelId = ParamsUtils.getChannelId(context)
        mImpl = SdkGenerator.getSdkInstance(channelId)
    }

    fun attachBaseContext(application: Application, context: Context) {
        if (mImpl is IApplication) {
            (mImpl as IApplication).attachBaseContext(application, context)
        }
    }

    fun initApplication(application: Application) {
        if (mImpl is IApplication) {
            (mImpl as IApplication).initApplication(application)
        }
    }

    fun initialize(activity: Activity, isLandscape: Boolean, callback: ICallback) {
        mImpl?.initialize(activity, isLandscape) { code, result ->
            callback.onResult(code, result)
        }
    }

    fun login(activity: Activity, callback: ICallback) {
        mImpl?.login(activity) { code, result ->
            if (code == 0) {
                SdkRequest.instance.userVerify(activity, result) { resultInfo ->
                    if (resultInfo.code == 1) {
                        if (!TextUtils.isEmpty(resultInfo.data)) {
                            val jsonObject = JSONObject(resultInfo.data)
                            SdkLoginInfo.instance.userId = jsonObject.getString("uid")
                            SdkLoginInfo.instance.token = jsonObject.getString("token")
                        }
                        callback.onResult(0, SdkLoginInfo.instance.toJsonString())
                    } else {
                        callback.onResult(-1, resultInfo.msg)
                    }
                }
            } else {
                callback.onResult(-1, result)
            }
        }
    }

    fun logout(activity: Activity, callback: ICallback) {
        mImpl?.logout(activity) { code, result ->
            callback.onResult(code, result)
        }
    }

    fun charge(activity: Activity, chargeInfo: SdkChargeInfo, isIab: Boolean, callback: ICallback) {
        // 判断渠道
        if (channelId == ChannelId.YYXX || channelId == ChannelId.HUAWEI) {
            Logger.d("特殊渠道先生成渠道订单号")
            if (mImpl is IOrder) {
                (mImpl as IOrder).getOrderId(activity, chargeInfo) {
                    mImpl?.charge(activity, chargeInfo, isIab) { code, result ->
                        callback.onResult(code, result)
                    }
                }
            } else {
                callback.onResult(0, "获取充值信息异常")
            }
        } else {
            SdkRequest.instance.createOrder(activity, "", chargeInfo) { resultInfo ->
                if (TextUtils.isEmpty(resultInfo.data)) {
                    callback.onResult(-1, "生成订单异常")
                    return@createOrder
                }
                val jsonObject = JSONObject(resultInfo.data)
                chargeInfo.orderId = jsonObject.getString("order_id")
                mImpl?.charge(activity, chargeInfo, isIab) { code, result ->
                    callback.onResult(code, result)
                }
            }
        }
    }

    fun roleCreate(activity: Activity, roleInfo: SdkRoleInfo) {
        if (mImpl is IRoleData) {
            (mImpl as IRoleData).roleCreate(activity, roleInfo)
        }
    }

    fun roleLauncher(activity: Activity, roleInfo: SdkRoleInfo) {
        if (mImpl is IRoleData) {
            (mImpl as IRoleData).roleLauncher(activity, roleInfo)
        }
    }

    fun roleLevelUp(activity: Activity, roleInfo: SdkRoleInfo) {
        if (mImpl is IRoleData) {
            (mImpl as IRoleData).roleUpgrade(activity, roleInfo)
        }
    }

    fun hasExitView(activity: Activity): Boolean = if (mImpl == null) {
        false
    } else {
        mImpl!!.hasExitView(activity)
    }

    fun openExitView(activity: Activity, callback: ICallback) {
        mImpl?.openExitView(activity) { code, result ->
            callback.onResult(code, result)
        }
    }

    fun getChannelVersion(): String = if (mImpl == null) {
        ""
    } else {
        mImpl!!.getChannelVersion()
    }

    fun getChannelName(): String = if (mImpl == null) {
        ""
    } else {
        mImpl!!.getChannelName()
    }

    fun onStart(activity: Activity) {
        if (mImpl is ILifeCycle) {
            (mImpl as ILifeCycle).onStart(activity)
        }
    }

    fun onRestart(activity: Activity) {
        if (mImpl is ILifeCycle) {
            (mImpl as ILifeCycle).onReStart(activity)
        }
    }

    fun onResume(activity: Activity) {
        if (mImpl is ILifeCycle) {
            (mImpl as ILifeCycle).onResume(activity)
        }
    }

    fun onPause(activity: Activity) {
        if (mImpl is ILifeCycle) {
            (mImpl as ILifeCycle).onPause(activity)
        }
    }

    fun onStop(activity: Activity) {
        if (mImpl is ILifeCycle) {
            (mImpl as ILifeCycle).onStop(activity)
        }
    }

    fun onDestroy(activity: Activity) {
        mImpl?.onDestroy(activity)
    }

    fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) {
        if (mImpl is ILifeCycle) {
            (mImpl as ILifeCycle).onActivityResult(activity, requestCode, resultCode, data)
        }
    }

    fun onNewIntent(activity: Activity, intent: Intent?) {
        if (mImpl is ILifeCycle) {
            (mImpl as ILifeCycle).onNewIntent(activity, intent)
        }
    }
}