package com.kdanmobile.reader.screen.view

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_reading_mode_select.view.*

class ReadingModeSelectView : ConstraintLayout {
    companion object {
        const val modeDay = 0
        const val modeNight = 1
        const val modeSoft = 2
        const val modeChromeWhite = 3
        const val modeEtonBlue = 4
        const val modeWhiteLilac = 5
    }

    interface OnReadModeSelectListener {
        fun onReadModeSelect(readMode: Int)
    }

    interface OnClickLockedItemListener {
        fun onClickLockedItem()
    }

    var onClickLockedItemListener: OnClickLockedItemListener? = null
    var onReadModeSelectListener: OnReadModeSelectListener? = null

    var currentReadMode: Int? = null
        set(value) {
            field = value
            when (value) {
                modeDay -> setReadModeBtnActivated(btn_readingMode_day)
                modeNight -> setReadModeBtnActivated(btn_readingMode_night)
                modeSoft -> setReadModeBtnActivated(btn_readingMode_soft)
                modeChromeWhite -> setReadModeBtnActivated(btn_readingMode_chromeWhite)
                modeEtonBlue -> setReadModeBtnActivated(btn_readingMode_etonBlue)
                modeWhiteLilac -> setReadModeBtnActivated(btn_readingMode_whiteLilac)
            }
        }

    var isLock: Boolean = false
        set(value) {
            field = value
            setReadModeBtnLockVisible(field)
        }

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        initView()
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.view_reading_mode_select, this).apply {
            btn_readingMode_day.setOnClickListener {
                onReadModeSelectListener?.onReadModeSelect(modeDay)
                setReadModeBtnActivated(it)
            }
            btn_readingMode_chromeWhite.setOnClickListener {
                onReadModeSelectListener?.onReadModeSelect(modeChromeWhite)
                setReadModeBtnActivated(it)
            }
            btn_readingMode_etonBlue.setOnClickListener {
                onReadModeSelectListener?.onReadModeSelect(modeEtonBlue)
                setReadModeBtnActivated(it)
            }
            btn_readingMode_whiteLilac.setOnClickListener {
                onReadModeSelectListener?.onReadModeSelect(modeWhiteLilac)
                setReadModeBtnActivated(it)
            }
            btn_readingMode_night.setOnClickListener {
                if (!isLock) {
                    onReadModeSelectListener?.onReadModeSelect(modeNight)
                    setReadModeBtnActivated(it)
                } else {
                    onClickLockedItemListener?.onClickLockedItem()
                }
            }
            btn_readingMode_soft.setOnClickListener {
                if (!isLock) {
                    onReadModeSelectListener?.onReadModeSelect(modeSoft)
                    setReadModeBtnActivated(it)
                } else {
                    onClickLockedItemListener?.onClickLockedItem()
                }
            }
            setReadModeBtnLockVisible(isLock)
        }
    }

    private fun setReadModeBtnActivated(view: View?) {
        btn_readingMode_day?.isActivated = false
        btn_readingMode_chromeWhite?.isActivated = false
        btn_readingMode_etonBlue?.isActivated = false
        btn_readingMode_whiteLilac?.isActivated = false
        btn_readingMode_night?.isActivated = false
        btn_readingMode_soft?.isActivated = false
        view?.isActivated = true
    }

    private fun setReadModeBtnLockVisible(isVisible: Boolean) {
        if (isVisible) {
            iv_readingMode_night_lock?.visibility = View.VISIBLE
            iv_readingMode_soft_lock?.visibility = View.VISIBLE
        } else {
            iv_readingMode_night_lock?.visibility = View.INVISIBLE
            iv_readingMode_soft_lock?.visibility = View.INVISIBLE
        }
    }
}