package com.kdanmobile.reader.setting

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.widget.CompoundButton
import com.kdanmobile.reader.R
import com.kdanmobile.reader.screen.model.SharePrefsModel
import com.kdanmobile.reader.screen.view.ReadingModeSelectView
import com.kdanmobile.reader.screen.view.TurnPageModeSelectView
import kotlinx.android.synthetic.main.activity_reader_setting.*

@Deprecated("Please use ReaderSettingDialogFragment")
class ReaderSettingActivity : AppCompatActivity() {

    private lateinit var viewModel: ReaderSettingViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reader_setting)

        viewModel = ViewModelProviders.of(this, ReaderSettingViewModelFactory(SharePrefsModel(applicationContext)))
                .get(ReaderSettingViewModel::class.java)

        viewModel.readMode.observe(this, Observer(this::onReadModeChanged))
        viewModel.isCropModeEnable.observe(this, Observer(this::onCropModeEnableChanged))
        viewModel.isShowStateBarEnable.observe(this, Observer(this::onShowStateBarEnableChanged))
        viewModel.turnPageDirection.observe(this, Observer(this::onTurnPageDirectionChanged))
        viewModel.isShowTurnPageBtnEnable.observe(this, Observer(this::onShowTurnPageBtnEnableChange))
        viewModel.isLockScreenSleepEnable.observe(this, Observer(this::onLockScreenSleepEnableChange))
        viewModel.isShowCommentToolEnable.observe(this, Observer(this::onShowCommentToolEnableChange))
        viewModel.isShowBOTAToolEnable.observe(this, Observer(this::onShowBOTAToolEnableChange))

        setupToolbar()
        setupReadingModeView()
        setupTurnPageView()
        setupScreenSettingView()
        setupToolbarSettingView()

        rowSwitchView_smartCrop.onCheckedChangeListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
            viewModel.setCropModeEnable(isChecked)
        }
        rowSwitchView_showStateBar.onCheckedChangeListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
            viewModel.setShowStateBarEnable(isChecked)
        }
    }

    private fun setupToolbar() {
        setSupportActionBar(toolbar_viewerSetting)
        supportActionBar?.setDisplayShowTitleEnabled(false)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }

    private fun setupReadingModeView() {
        readingModeSelectView.isLock = true
        readingModeSelectView.onReadModeSelectListener = object : ReadingModeSelectView.OnReadModeSelectListener {
            override fun onReadModeSelect(readMode: Int) {
                viewModel.setReadMode(readMode)
            }
        }
    }

    private fun setupTurnPageView() {
        turnPageModeSelectView.onTurnPageModeChangedListener = object : TurnPageModeSelectView.OnTurnPageModeChangedListener {
            override fun onTurnPageModeChanged(direction: Int) {
                viewModel.setTurnPageDirection(direction)
            }
        }
        turnPageModeSelectView.onShowTurnPageBtnEnableChangedListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
            viewModel.setTurnPageButtonEnable(isChecked)
        }
    }

    private fun setupScreenSettingView() {
        screenSettingView.onCheckedChangeListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
            viewModel.setLockScreenSleepEnable(isChecked)
        }
    }

    private fun setupToolbarSettingView() {
        toolbarSettingView.onCheckedChangeListenerComment = CompoundButton.OnCheckedChangeListener { _, isChecked ->
            viewModel.setShowCommentToolEnable(isChecked)
        }
        toolbarSettingView.onCheckedChangeListenerBOTA = CompoundButton.OnCheckedChangeListener { _, isChecked ->
            viewModel.setShowBOTAToolEnable(isChecked)
        }
    }

    private fun onReadModeChanged(readMode: Int?) {
        readMode?.let {
            readingModeSelectView.currentReadMode = it
        }
    }

    private fun onCropModeEnableChanged(enable: Boolean?) {
        enable?.let {
            rowSwitchView_smartCrop.isChecked = it
        }
    }

    private fun onShowStateBarEnableChanged(enable: Boolean?) {
        enable?.let {
            rowSwitchView_showStateBar.isChecked = it
        }
    }

    private fun onTurnPageDirectionChanged(direction: Int?) {
        direction?.let {
            turnPageModeSelectView.direction = it
        }
    }

    private fun onShowTurnPageBtnEnableChange(enable: Boolean?) {
        enable?.let {
            turnPageModeSelectView.isTurnPageBtnEnable = it
        }
    }

    private fun onLockScreenSleepEnableChange(enable: Boolean?) {
        enable?.let {
            screenSettingView.isLockScreenSleepEnable = it
        }
    }

    private fun onShowCommentToolEnableChange(enable: Boolean?) {
        enable?.let {
            toolbarSettingView.isShowCommentToolEnable = it
        }
    }

    private fun onShowBOTAToolEnableChange(enable: Boolean?) {
        enable?.let {
            toolbarSettingView.isShowBOTAToolEnable = it
        }
    }
}
