package com.bc_chat.im.activity

import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.im.R
import com.bc_chat.im.databinding.ActivityForwordBinding
import com.bc_chat.im.fragment.forword.ForwardChatFragment
import com.bc_chat.im.fragment.forword.ForwardContactFragment
import com.zhaohaoting.framework.abs.AbsV4Fragment
import com.zhaohaoting.framework.abs.BaseNoActionBarActivity
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.utils.observer.Observer
import com.zhaohaoting.framework.utils.observer.ObserverManager
import io.rong.imlib.model.Message
import org.jetbrains.anko.bundleOf

/**
 * author：Created by zhaohaoting on 2019/3/14
 * email：526309416@qq.com
 * desc：转发（分享）
 */
@Route(path = RouteConfig.FORWARD_ACTIVITY)
class ForwardActivity : BaseNoActionBarActivity<BaseContract.BasePresenter>(), Observer<String> {

    private val binding by lazy {
        rootDataBinding as ActivityForwordBinding
    }

    private val fragmentArray: Array<Class<out AbsV4Fragment<BaseContract.BasePresenter>>> = arrayOf(ForwardChatFragment::class.java, ForwardContactFragment::class.java)
    private val fragmentTags: Array<String> = arrayOf("ForwardChatFragment", "ForwardContactFragment")
    private val fragmentNames: Array<String> = arrayOf("会话", "联系人")
    private val textBgs: Array<Int> = arrayOf(R.drawable.selector_forward_left_bg, R.drawable.selector_forward_right_bg)

    override fun initPresenter(): BaseContract.BasePresenter? = null

    override fun getLayoutResId(): Int = R.layout.activity_forword
    override fun onInit() {
        val forwardMegs = intent.getParcelableArrayListExtra<Message>("selectList")
        setLightMode(resources.getColor(R.color.title_bg_color), true,false)
        binding.tabhost.setup(this, supportFragmentManager, android.R.id.tabcontent)
        fragmentArray.forEachIndexed { i: Int, clazz: Class<out AbsV4Fragment<BaseContract.BasePresenter>> ->
            val tabSpec = binding.tabhost.newTabSpec(fragmentTags[i]).setIndicator(getTabItemView(i))
            binding.tabhost.addTab(tabSpec, clazz, bundleOf("selectList" to forwardMegs))
            binding.tabhost.tabWidget.dividerDrawable = null
        }
        binding.tvBack.setOnClickListener {
            finish()
        }
        ObserverManager.getInstance().registerObserver(Const.ObserverKey.FORWARD_MESSAGE, this)
    }

    override fun onEvent(key: String?, var1: String?) {
        finish()
    }

    private fun getTabItemView(i: Int): View? {
        val textView = layoutInflater.inflate(R.layout.forward_tab_layout, null) as TextView
        textView.text = fragmentNames[i]
        textView.setBackgroundResource(textBgs[i])
        return textView
    }

    private fun <T : AbsV4Fragment<*>> getChildFragment(tag: Int): T? {
        return supportFragmentManager.findFragmentByTag(tag.toString()) as T?
    }

    override fun onDestroy() {
        super.onDestroy()
        ObserverManager.getInstance().unregisterObserver(Const.ObserverKey.FORWARD_MESSAGE, this)
    }
}
