package com.ydl.ydlcommon.base

import android.app.Activity
import android.content.res.Resources
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.trello.rxlifecycle2.android.ActivityEvent
import com.ydl.ydlcommon.base.lifecycle.IActivityLifecycleable
import com.ydl.ydlcommon.bean.StatusBarOptions
import com.ydl.ydlcommon.ui.LoadingDialogFragment
import com.ydl.ydlcommon.utils.ActivityManager
import com.ydl.ydlcommon.utils.AndroidSystemHelper
import com.ydl.ydlcommon.utils.StatusBarUtils
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import kotlin.properties.Delegates

/**
 * 基础 Activity，所有的 Activity 都继承此类
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/01/27
 */
abstract class BaseActivity : AppCompatActivity(),IActivityLifecycleable {

    private val mLifecycleSubject = BehaviorSubject.create<ActivityEvent>()
    var mContext: Activity by Delegates.notNull()
    var statusView: View? = null
    private var loadingDialogFragment: LoadingDialogFragment? = null

    override fun provideLifecycleSubject(): Subject<ActivityEvent> {
        return mLifecycleSubject;
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        //修复Android 8.0 崩溃
        AndroidSystemHelper.fixAndroidOrientationBug(this)
        super.onCreate(savedInstanceState)

        Log.d("TAG", javaClass.name)
        mContext = this

        val layoutResId = layoutResId()
        if (layoutResId != 0) setContentView(layoutResId())
        ActivityManager.getInstance().addStack(this)
        initDataAndEvent()
    }

    override fun setRequestedOrientation(requestedOrientation: Int) {
        if (AndroidSystemHelper.isAllowSetOrientation(this)) {
            super.setRequestedOrientation(requestedOrientation)
        }
    }

    override fun setContentView(@LayoutRes layoutResID: Int) {
        if (getStatusViewOptions()?.isAddStatusView) {
            val returnViews = StatusBarUtils.initStatusBarView(this, layoutResID, getStatusViewOptions())

            if(returnViews.size > 1){
                statusView = returnViews[1];
            }

            super.setContentView(returnViews[0])
            AndroidSystemHelper.fixAndroidBug5497Workaround(this)
        } else {
            super.setContentView(layoutResID)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityManager.getInstance().removeStack(this)
        AndroidSystemHelper.fixInputMethodManagerLeak(this)
    }

    @LayoutRes
    protected abstract fun layoutResId(): Int

    // 初始化数据和事件
    protected abstract fun initDataAndEvent()

    override fun getResources(): Resources {
        //解决 修改系统字体大小后 H5页面大小比例不正常现象
        val res = super.getResources()
        return AndroidSystemHelper.fixResourcesScale(res)
    }

    open fun getStatusViewOptions():StatusBarOptions{
        return StatusBarOptions()
    }


    fun showProgressDialog() {
        showProgressDialog("")
    }


    //显示dialog
    open fun showProgressDialog(str: String?) {
        if (loadingDialogFragment == null) {
            loadingDialogFragment = LoadingDialogFragment.newInstance(str)
        }
        if (loadingDialogFragment?.isAdded!!) {
            return
        }
        loadingDialogFragment?.show(supportFragmentManager, BaseActivity::class.java.simpleName)
    }

    //移除dialog
    open fun dismissProgressDialog() {
        try {
            if (loadingDialogFragment != null && loadingDialogFragment?.isAdded!!) {
                loadingDialogFragment?.hide()
                loadingDialogFragment = null
            }
        } catch (e: Exception) {
            //防止以下错误
            //java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        }

    }

}