package cn.yyxx.eyuancomm.impl

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import cn.yyxx.eyuancomm.comm.entity.SdkChargeInfo
import cn.yyxx.eyuancomm.comm.entity.SdkRoleInfo
import cn.yyxx.eyuancomm.comm.internal.ICallback
import cn.yyxx.eyuancomm.comm.internal.feature.*

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

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

    init {
        //TODO 配置文件获取渠道id
        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 ->
            callback.onResult(code, result)
        }
    }

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

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

    fun getOrderExt(activity: Activity, chargeInfo: SdkChargeInfo, callback: ICallback) {
        if (mImpl is IOrder) {
            (mImpl as IOrder).getOrderExt(activity, chargeInfo) { code, result ->
                callback.onResult(code, result)
            }
        } else {
            callback.onResult(-1, "获取订单扩展参数异常")
        }
    }

    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)
        }
    }

    fun showFloatBall(activity: Activity) {
        if (mImpl is IFloatBall) {
            (mImpl as IFloatBall).showFloatBall(activity)
        }
    }

    fun hideFloatBall(activity: Activity) {
        if (mImpl is IFloatBall) {
            (mImpl as IFloatBall).hideFloatBall(activity)
        }
    }

    fun setOnFloatBallLogout(callback: ICallback) {
        if (mImpl is IFloatBall) {
            (mImpl as IFloatBall).onFloatBallLogout { code, result ->
                callback.onResult(code, result)
            }
        }
    }
}