package com.ydl.ydlcommon.mvp.lce

import android.support.annotation.CallSuper
import android.view.View
import com.ydl.ydlcommon.R
import com.ydl.ydlcommon.base.BaseMvpActivity
import com.ydl.ydlcommon.mvp.base.IPresenter
import com.ydl.ydlcommon.mvp.base.IView
import com.ydl.ydlcommon.mvp.lce.view.ILceViewState

/**
 * Created by haorui on 2019-08-14.
 * Des: LCE 架构 BaseActivity，统一处理页面加载状态
 *      LCE：L：Loading加载，C：Content加载内容，E：Error加载失败
 *
 *     【注意：StateView需设置id为：R.id.lce_state_view】
 *     【注意：ContentView需设置id为：R.id.lce_content_view】
 */
abstract class BaseLceActivity<V : IView, P : IPresenter<V>> : BaseMvpActivity<V, P>(), ILceView{

    protected lateinit var lceStateView: ILceViewState
    protected var lceContentView: View? = null

    @CallSuper
    override fun onContentChanged() {
        super.onContentChanged()
        lceStateView = createStateView()
        lceContentView = createContentView()

        if (lceContentView == null) {
            throw NullPointerException(
                "请检查布局文件中ContentView是否设置，id需为R.id.lce_content_view"
            )
        }
    }

    protected fun createStateView(): ILceViewState {
        val stateView = findViewById<View>(R.id.lce_state_view)
        if (stateView is ILceViewState) {
            val viewState = stateView as ILceViewState
            viewState.setListener(object : ILceViewState.LogoLoadingListener {
                override fun onDataResetClick() {
                    loadData()
                }

                override fun onBackClick() {
                    this@BaseLceActivity.onBackPressed()
                }
            })
            return stateView
        }

        throw NullPointerException(
            "请检查布局文件中StateView是否设置，id需为R.id.lce_state_view"
        )
    }

    protected fun createContentView(): View {
        return findViewById(R.id.lce_content_view)
    }

    override fun showLoading() {
        lceStateView.showLoading()
    }

    override fun hideLoading() {
        lceStateView.hideLoading()
    }

    override fun showContent() {
        lceContentView?.visibility = View.VISIBLE
        lceStateView.hideLoading()
    }

    override fun showEmpty(msg: String) {
        lceStateView.showEmpty(msg)
    }

    override fun showError(msg: String) {
        lceStateView.showError(msg)
    }

}


