package com.bc_chat.circle.activity

import android.os.Bundle
import android.support.v4.view.ViewPager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.bc_chat.bc_base.entity.contact.CommentMine
import com.bc_chat.bc_base.ui.BaseTabActivity
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.bc_base.utils.MessageHelper
import com.bc_chat.bc_base.utils.gone
import com.bc_chat.bc_base.utils.visible
import com.bc_chat.circle.activity.fragment.CommentMineFragment
import com.bc_chat.contacts.R
import com.zhaohaoting.framework.abs.contract.BaseContract
import com.zhaohaoting.framework.abs.entity.TabTag
import com.zhaohaoting.framework.abs.entity.Tag
import com.zhaohaoting.framework.mvchelper.mvc.IDataAdapter
import com.zhaohaoting.framework.mvchelper.mvc.OnRefreshEndListener
import com.zhaohaoting.framework.utils.observer.Observer
import com.zhaohaoting.framework.utils.observer.ObserverManager
import org.jetbrains.anko.bundleOf

class CircleMessageActivity : BaseTabActivity<BaseContract.BasePresenter>(), Observer<String> {

    private var inflater: LayoutInflater? = null

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

    override fun initialize() {
        isTouchHide = false
        actionBar.setNavigationIcon(R.mipmap.back)
        inflater = LayoutInflater.from(this)
        isCustomTab = true
        ObserverManager.getInstance().registerObserver(Const.ObserverKey.TOPIC_COMMENT, this)
        ObserverManager.getInstance().registerObserver(Const.ObserverKey.TOPIC_CHILD_COMMENT, this)
        viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                selectShowNewMsg(position)
            }
        })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        fragmentList.mapIndexed { index, absV4Fragment ->
            (absV4Fragment as CommentMineFragment).setOnRefreshEndListener(OnRefreshEndListener { adapter, result ->
                if (index == 0) {
                    MessageHelper.instance.isNewComment = false
                    showNewMsg(0)
                } else {
                    MessageHelper.instance.isChildNewComment = false
                    showNewMsg(1)
                }
            })
        }
    }

    override fun onResume() {
        super.onResume()
        showNewMsg(0)
        showNewMsg(1)
    }

    private fun selectShowNewMsg(position: Int) {
        val view = tabLayout.getTabAt(position)?.customView
        val textView = view?.findViewById<TextView>(R.id.un_read)
        if (position == 1) {
            if (MessageHelper.instance.isChildNewComment) {
                MessageHelper.instance.isChildNewComment = false
            }
            textView.gone()
        } else {
            if (MessageHelper.instance.isNewComment) {
                MessageHelper.instance.isNewComment = false
            }
            textView.gone()
        }
    }

    private fun showNewMsg(position: Int) {
        val view = tabLayout.getTabAt(position)?.customView
        val textView = view?.findViewById<TextView>(R.id.un_read)
        if (position == 1) {
            if (MessageHelper.instance.isChildNewComment) {
                textView.visible()
            } else {
                textView.gone()
            }
        } else {
            if (MessageHelper.instance.isNewComment) {
                textView.visible()
            } else {
                textView.gone()
            }
        }
    }

    override fun onEvent(key: String?, var1: String?) {
        if (TextUtils.equals(Const.ObserverKey.TOPIC_COMMENT, key)) {
            MessageHelper.instance.isNewComment = true
            showNewMsg(0)
        }
        if (TextUtils.equals(Const.ObserverKey.TOPIC_CHILD_COMMENT, key)) {
            MessageHelper.instance.isChildNewComment = true
            showNewMsg(1)
        }
    }

    override fun getTabNames(): MutableList<TabTag> = mutableListOf(TabTag("评论我的", "1"), TabTag("回复我的", "2"))

    override fun getFragmentList(): MutableList<CommentMineFragment> {
        val mutableListOf: MutableList<CommentMineFragment> = mutableListOf()
        tabNames?.map {
            val mineFragment = CommentMineFragment()
            mineFragment.arguments = bundleOf("type" to it.id)
            mutableListOf.add(mineFragment)
        }
        return mutableListOf
    }

    override fun getTabItemView(tag: Tag?): View? {
        val view = inflater?.inflate(R.layout.circle_tab_layout, null, false)
        val tvText = view?.findViewById<TextView>(R.id.tv_text)
        val unRead = view?.findViewById<TextView>(R.id.un_read)
        tvText?.text = tag?.name
        return view!!
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            fragmentList.map {
                if (it.userVisibleHint)
                    (it as CommentMineFragment).dispatchTouchEvent(ev)
            }
            return super.dispatchTouchEvent(ev)
        }
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

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