package com.hanwha.morakmorak.ui.main

import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayout
import com.hanwha.morakmorak.R
import com.hanwha.morakmorak.databinding.ActivityMainBinding
import com.hanwha.morakmorak.ui.BindingActivity
import com.hanwha.morakmorak.ui.main.home.post.HomePostActivity
import com.hanwha.morakmorak.util.ViewUtils
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar_main.*
import kotlinx.android.synthetic.main.toolbar_main.view.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

class MainActivity : BindingActivity<ActivityMainBinding, MainViewModel>() {

    override fun getLayoutResId() = R.layout.activity_main

    override fun getMyViewModel(): MainViewModel = getViewModel()

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

        setSupportActionBar(binding.toolbarMain.mainToolbar)

        binding.run {
            lifecycleOwner = this@MainActivity
            fm = supportFragmentManager
            vm = viewModel.apply {
                toolbarLeftFirstClick = {
                    Snackbar.make(binding.snackbarCoordinatorLayout,
                            "돈 주면 만들어 줌",
                            Snackbar.LENGTH_SHORT).show()
                }
                toolbarLeftSecondClick = {
                    Snackbar.make(binding.snackbarCoordinatorLayout,
                            "돈 주면 만들어 줌",
                            Snackbar.LENGTH_SHORT).show()
                }
                toolbarRightFirstClick = {
                    if (main_viewPager.currentItem == TAB_HOME || mainViewPager.currentItem == TAB_MYPAGE) {
                        val intent = Intent(this@MainActivity, HomePostActivity::class.java)
                        startActivity(intent)
                    } else {
                        Snackbar.make(
                                snackbarCoordinatorLayout,
                                "돈 주면 만들어 줌",
                                Snackbar.LENGTH_SHORT)
                                .show()
                    }
                }
            }
        }

        // ViewPager, Tab 설정
        configTabLayout()
    }

    /**
     * 탭, 뷰페이저 설정
     */
    private fun configTabLayout() {
        main_tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                main_viewPager.currentItem = tab!!.position
                when (tab.position) {
                    TAB_PEOPLE -> main_toolbar.right_first_imgBtn.setImageResource(R.drawable.ic_vector_add_people)
                    TAB_CHAT -> main_toolbar.right_first_imgBtn.setImageResource(R.drawable.ic_vector_add_chat)
                    else -> main_toolbar.right_first_imgBtn.setImageResource(R.drawable.btn_pencil)
                }
                setToolbarRightBtnLayout(tab.position)
            }
        })

        main_viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(main_tabLayout))
    }

    /**
     * 툴바 우측 버튼 레이아웃 크기 설정
     */
    private fun setToolbarRightBtnLayout(position: Int) {
        val width: Int
        val height: Int

        when (position) {
            TAB_PEOPLE, TAB_CHAT -> { width = ViewUtils.dpToPx(23f); height = ViewUtils.dpToPx(22f) }
            else -> { width = ViewUtils.dpToPx(20f); height = ViewUtils.dpToPx(24f) }
        }

        val layoutParam = main_toolbar.right_first_imgBtn.layoutParams
        layoutParam.width = width
        layoutParam.height = height
        main_toolbar.right_first_imgBtn.layoutParams = layoutParam
    }

    companion object {
        const val TAB_HOME = 0
        const val TAB_PEOPLE = 1
        const val TAB_CHAT = 2
        const val TAB_MYPAGE = 3
    }

}
