package com.dahe.gldriver.temp

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.dahe.gldriver.temp.Constant.UI_TYPE
import com.nirvana.tools.core.AppUtils
import com.umeng.umverify.UMVerifyHelper

abstract class BaseUIConfig(@JvmField var mActivity: Activity, @JvmField var mAuthHelper: UMVerifyHelper) :
    AuthPageConfig {
    @JvmField
    var mContext: Context
    var mScreenWidthDp = 0
    var mScreenHeightDp = 0

    init {
        mContext = mActivity.applicationContext
    }

    protected fun initSwitchView(marginTop: Int): View {
        val switchTV = TextView(mContext)
        val mLayoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            AppUtils.dp2px(mContext, 50f)
        )
        //一键登录按钮默认marginTop 270dp
        mLayoutParams.setMargins(0, AppUtils.dp2px(mContext, marginTop.toFloat()), 0, 0)
        mLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE)
        switchTV.text = "切换短信验证码登录"
        switchTV.setTextColor(Color.BLACK)
        switchTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13.0f)
        switchTV.layoutParams = mLayoutParams
        return switchTV
    }
    //    protected void updateScreenSize(int authPageScreenOrientation) {
    //        int screenHeightDp = AppUtils.px2dp(mContext, AppUtils.getPhoneHeightPixels(mContext));
    //        int screenWidthDp = AppUtils.px2dp(mContext, AppUtils.getPhoneWidthPixels(mContext));
    //        int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
    //        if (authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
    //            authPageScreenOrientation = mActivity.getRequestedOrientation();
    //        }
    //        if (authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    //                || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
    //                || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE) {
    //            rotation = Surface.ROTATION_90;
    //        } else if (authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    //                || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
    //                || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT) {
    //            rotation = Surface.ROTATION_180;
    //        }
    //        switch (rotation) {
    //            case Surface.ROTATION_0:
    //            case Surface.ROTATION_180:
    //                mScreenWidthDp = screenWidthDp;
    //                mScreenHeightDp = screenHeightDp;
    //                break;
    //            case Surface.ROTATION_90:
    //            case Surface.ROTATION_270:
    //                mScreenWidthDp = screenHeightDp;
    //                mScreenHeightDp = screenWidthDp;
    //                break;
    //            default:
    //                break;
    //        }
    //    }
    /**
     * 在横屏APP弹竖屏一键登录页面或者竖屏APP弹横屏授权页时处理特殊逻辑
     * Android8.0只能启动SCREEN_ORIENTATION_BEHIND模式的Activity
     */
    override fun onResume() {}
    override fun release() {
        mAuthHelper.releasePreLoginResultListener()
        mAuthHelper.setAuthListener(null)
        mAuthHelper.setUIClickListener(null)
        mAuthHelper.removeAuthRegisterViewConfig()
        mAuthHelper.removeAuthRegisterXmlConfig()
    }

    companion object {
        fun init(
            type: UI_TYPE?,
            activity: Activity?,
            authHelper: UMVerifyHelper?
        ): AuthPageConfig? {
            return when (type) {
                UI_TYPE.FULL_PORT -> FullPortConfig(activity, authHelper)
                else -> null
            }
        }
    }
}
