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 modeSefia = 2
        const val modeLilac = 5
    }

    interface OnReadModeSelectListener {
        fun onReadModeSelect(readMode: Int)
    }

    var onReadModeSelectListener: OnReadModeSelectListener? = null

    var currentReadMode: Int? = null
        set(value) {
            field = value
            when (value) {
                modeDay -> setReadModeBtnActivated(btn_readingMode_day)
                modeNight -> setReadModeBtnActivated(btn_readingMode_night)
                modeSefia -> setReadModeBtnActivated(btn_readingMode_sefia)
                modeLilac -> setReadModeBtnActivated(btn_readingMode_lilac)
            }
        }

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

    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_lilac.setOnClickListener {
                onReadModeSelectListener?.onReadModeSelect(modeLilac)
                setReadModeBtnActivated(it)
            }
            btn_readingMode_night.setOnClickListener {
                if (!isLock) {
                    onReadModeSelectListener?.onReadModeSelect(modeNight)
                    setReadModeBtnActivated(it)
                }
            }
            btn_readingMode_sefia.setOnClickListener {
                if (!isLock) {
                    onReadModeSelectListener?.onReadModeSelect(modeSefia)
                    setReadModeBtnActivated(it)
                }
            }
            setReadModeBtnLockVisible(isLock)
        }
    }

    private fun setReadModeBtnActivated(view: View?) {
        btn_readingMode_day?.isActivated = false
        btn_readingMode_lilac?.isActivated = false
        btn_readingMode_night?.isActivated = false
        btn_readingMode_sefia?.isActivated = false
        view?.isActivated = true
    }

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