package kr.co.vividnext.sodalive.live.room.menu

import android.app.Service
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.jakewharton.rxbinding4.widget.textChanges
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.base.BaseActivity
import kr.co.vividnext.sodalive.base.SodaDialog
import kr.co.vividnext.sodalive.common.LoadingDialog
import kr.co.vividnext.sodalive.databinding.ActivityMenuConfigBinding
import kr.co.vividnext.sodalive.live.room.create.LiveRoomCreateViewModel
import org.koin.android.ext.android.inject
import java.util.concurrent.TimeUnit

class MenuConfigActivity : BaseActivity<ActivityMenuConfigBinding>(
    ActivityMenuConfigBinding::inflate
) {
    private val viewModel: MenuConfigViewModel by inject()

    private lateinit var imm: InputMethodManager
    private lateinit var loadingDialog: LoadingDialog

    private val handler = Handler(Looper.getMainLooper())

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

        bindData()
        viewModel.getAllMenuPreset()
    }

    override fun setupView() {
        binding.toolbar.tvBack.text = "메뉴 설정"
        binding.toolbar.tvBack.setOnClickListener { finish() }

        loadingDialog = LoadingDialog(this, layoutInflater)
        imm = getSystemService(
            Service.INPUT_METHOD_SERVICE
        ) as InputMethodManager

        binding.tvSave.setOnClickListener {
            handler.postDelayed({
                imm.hideSoftInputFromWindow(currentFocus?.windowToken, 0)
            }, 100)

            // 저장 액션
            viewModel.saveMenu()
        }

        binding.llSelectMenu1.setOnClickListener {
            viewModel.selectMenuPreset(LiveRoomCreateViewModel.SelectedMenu.MENU_1)
        }

        binding.llSelectMenu2.setOnClickListener {
            viewModel.selectMenuPreset(LiveRoomCreateViewModel.SelectedMenu.MENU_2)
        }

        binding.llSelectMenu3.setOnClickListener {
            viewModel.selectMenuPreset(LiveRoomCreateViewModel.SelectedMenu.MENU_3)
        }
    }

    private fun bindData() {
        viewModel.toastLiveData.observe(this) { it?.let { showToast(it) } }
        viewModel.isLoading.observe(this) {
            if (it) {
                loadingDialog.show(screenWidth)
            } else {
                loadingDialog.dismiss()
            }
        }

        viewModel.selectedMenuLiveData.observe(this) {
            deselectAllMenuPreset()

            when (it) {
                LiveRoomCreateViewModel.SelectedMenu.MENU_2 -> selectMenuPresetButton(
                    binding.ivSelectMenu2,
                    binding.llSelectMenu2,
                    binding.tvSelectMenu2
                )

                LiveRoomCreateViewModel.SelectedMenu.MENU_3 -> selectMenuPresetButton(
                    binding.ivSelectMenu3,
                    binding.llSelectMenu3,
                    binding.tvSelectMenu3
                )

                else -> selectMenuPresetButton(
                    binding.ivSelectMenu1,
                    binding.llSelectMenu1,
                    binding.tvSelectMenu1
                )
            }
        }

        viewModel.selectedMenuTextLiveData.observe(this) {
            binding.etMenu.setText(it)
        }

        compositeDisposable.add(
            binding.etMenu.textChanges().skip(1)
                .debounce(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe {
                    viewModel.menuText = it.toString()
                }
        )
    }

    private fun deselectAllMenuPreset() {
        binding.ivSelectMenu1.visibility = View.GONE
        binding.ivSelectMenu2.visibility = View.GONE
        binding.ivSelectMenu3.visibility = View.GONE

        binding.llSelectMenu1.setBackgroundResource(R.drawable.bg_round_corner_6_7_13181b)
        binding.tvSelectMenu1.setTextColor(
            ContextCompat.getColor(
                applicationContext,
                R.color.color_3bb9f1
            )
        )

        if (viewModel.countMenu() > 0) {
            binding.llSelectMenu2.setBackgroundResource(R.drawable.bg_round_corner_6_7_13181b)
            binding.tvSelectMenu2.setTextColor(
                ContextCompat.getColor(
                    applicationContext,
                    R.color.color_3bb9f1
                )
            )
        } else {
            binding.llSelectMenu2.setBackgroundResource(R.drawable.bg_round_corner_6_7_777777)
            binding.tvSelectMenu2.setTextColor(
                ContextCompat.getColor(
                    applicationContext,
                    R.color.color_555555
                )
            )
        }

        if (viewModel.countMenu() > 1) {
            binding.llSelectMenu3.setBackgroundResource(R.drawable.bg_round_corner_6_7_13181b)
            binding.tvSelectMenu3.setTextColor(
                ContextCompat.getColor(
                    applicationContext,
                    R.color.color_3bb9f1
                )
            )
        } else {
            binding.llSelectMenu3.setBackgroundResource(R.drawable.bg_round_corner_6_7_777777)
            binding.tvSelectMenu3.setTextColor(
                ContextCompat.getColor(
                    applicationContext,
                    R.color.color_555555
                )
            )
        }
    }

    private fun selectMenuPresetButton(
        ivSelectMenuPreset: ImageView,
        llSelectMenuPreset: LinearLayout,
        tvSelectMenuPreset: TextView
    ) {
        ivSelectMenuPreset.visibility = View.VISIBLE
        llSelectMenuPreset.setBackgroundResource(R.drawable.bg_round_corner_6_7_3bb9f1)
        tvSelectMenuPreset.setTextColor(
            ContextCompat.getColor(
                applicationContext,
                R.color.color_eeeeee
            )
        )
    }
}
