package com.hanwha.morakmorak.ui.main.home.detail

import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import com.google.android.material.tabs.TabLayout
import com.hanwha.morakmorak.R
import com.hanwha.morakmorak.databinding.ActivityHomeDetailBinding
import com.hanwha.morakmorak.ui.BindingActivity
import com.hanwha.morakmorak.ui.main.home.HomeFragment
import kotlinx.android.synthetic.main.activity_home_detail.*
import org.koin.androidx.viewmodel.ext.android.getViewModel
import timber.log.Timber

/**
 * Created by aaron on 10/09/2019
 */
class HomeDetailActivity : BindingActivity<ActivityHomeDetailBinding, HomeDetailViewModel>() {

    override fun getLayoutResId() = R.layout.activity_home_detail

    override fun getMyViewModel(): HomeDetailViewModel = getViewModel()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.run {
            lifecycleOwner = this@HomeDetailActivity
            fm = supportFragmentManager
            vm = viewModel.apply {
                toolbarLeftFirstClick = { finish() }
//                onJoinClick = {
//                    if (it is TextView) {
//                        when (it.tag) {
//                            null, DetailStatus.NONE -> setJoinStatus(DetailStatus.JOIN)
//                            DetailStatus.JOIN -> setJoinStatus(DetailStatus.NONE)
//                        }
//                    }
//                }
            }
            partyId = intent.getIntExtra(HomeFragment.EXTRA_PARTY_ID, 0)
            // Pager swipe 활성화 여부
            when (joinBtn.tag) {
                null, DetailStatus.NONE -> detailNonSwipeViewPager.swipeEnabled = false
                else -> detailNonSwipeViewPager.swipeEnabled = true
            }
        }
        setSupportActionBar(binding.toolbar.toolbarDetail)
        createTab()
        configViewPager()
    }

    private fun configViewPager() {
        detail_tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                Timber.i("onTabUnselected")
                val viewGroup = detail_tabLayout.getChildAt(0) as ViewGroup
                val currentTab = viewGroup.getChildAt(tab!!.position) as ViewGroup
                val title = currentTab.getChildAt(1)
                if (title is TextView) {
                    Timber.i("${title.text}")
                    title.setTypeface(null, Typeface.NORMAL)
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        title.setTextColor(getColor(R.color.white_three_80))
                    }
                }
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                Timber.i("onTabSelected")
                detail_nonSwipeViewPager.currentItem = tab!!.position
                val viewGroup = detail_tabLayout.getChildAt(0) as ViewGroup
                val currentTab = viewGroup.getChildAt(tab.position) as ViewGroup
                val title = currentTab.getChildAt(1)
                if (title is TextView) {
                    Timber.i("${title.text}")
                    title.setTypeface(title.typeface, Typeface.BOLD)
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        title.setTextColor(getColor(R.color.white_three))
                    }
                }
            }
        })

        detail_nonSwipeViewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(detail_tabLayout))
    }

    private fun createTab() {
        detail_tabLayout.addTab(detail_tabLayout.newTab().setText(getString(R.string.home_detail_tab_information)))
        detail_tabLayout.addTab(detail_tabLayout.newTab().setText(getString(R.string.home_detail_tab_chat)))
    }

    fun setJoinStatusObserver(currentStatus: LiveData<DetailStatus>) =
        currentStatus.observe(this, Observer { status -> setJoinStatus(status)})

    fun getUpdateObserver() = binding.vm!!.update

    private fun setJoinStatus(status: DetailStatus) {
        when (status) {
            DetailStatus.NONE -> {
//                join_btn.tag = DetailStatus.NONE
                binding.vm!!.isJoin = false
                join_btn.text = getString(R.string.home_detail_join_party)
                join_btn.setBackgroundResource(R.color.orange_pink_two)
                detail_tabLayout.visibility = View.GONE
                detail_nonSwipeViewPager.currentItem = 0
                detail_nonSwipeViewPager.swipeEnabled = false
            }
            DetailStatus.JOIN -> {
//                join_btn.tag = DetailStatus.JOIN
                binding.vm!!.isJoin = true
                join_btn.text = getString(R.string.home_detail_cancel_party)
                join_btn.setBackgroundResource(R.color.greyish_two)
                detail_tabLayout.visibility = View.VISIBLE
                detail_nonSwipeViewPager.swipeEnabled = true
            }
        }
    }

}