package com.christian

import android.os.Build
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import com.christian.common.CommonApp
import com.christian.common.immersiveToolbar
import com.christian.nav.canScrollDown
import com.christian.nav.canScrollUp
import com.christian.swipe.SwipeBackActivity
import eightbitlab.com.blurview.makeViewBlurExtendsFrameLayout
import kotlinx.android.synthetic.main.activity_detail_me.*
import kotlinx.android.synthetic.main.activity_tb_immersive.*
import me.everything.android.ui.overscroll.HorizontalOverScrollBounceEffectDecorator
import me.everything.android.ui.overscroll.adapters.IOverScrollDecoratorAdapter
import org.jetbrains.anko.dip

abstract class ImmersiveTopBarActivity : SwipeBackActivity() {

    var isBackMenu = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        initView()
    }

    override fun onResume() {
        super.onResume()
        tb_immersive_abl.post {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                tb_immersive_abl.elevation = dip(4).toFloat()
            }
        }
    }

    protected open fun initView() {
        if (isBackMenu) {
            this.setSupportActionBar(tb_immersive_toolbar)
            this.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        initToolbar()
        val tabTitleList: ArrayList<String> = arrayListOf(
            CommonApp.context.getString(R.string.tab_english),
            CommonApp.context.getString(R.string.tab_chinese),
        )
        initVp(tabTitleList)
    }

    abstract fun initVp(tabTitleList: java.util.ArrayList<String>)

    private fun initToolbar() {
        immersiveToolbar(this, tb_immersive_toolbar)
        makeViewBlurExtendsFrameLayout(tb_immersive_fl, detail_me_wrapper, window)
        HorizontalOverScrollBounceEffectDecorator(object : IOverScrollDecoratorAdapter {
            override fun isInAbsoluteEnd(): Boolean {
                return !tb_immersive_tl.canScrollDown()
            }

            override fun isInAbsoluteStart(): Boolean {
                return !tb_immersive_tl.canScrollUp()
            }

            override fun getView(): View {
                return tb_immersive_tl
            }

        })
    }

    abstract fun getLayoutId(): Int

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        com.christian.util.dispatchTouchEvent(this, ev)
        return super.dispatchTouchEvent(ev)
    }
}