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.widget.CompoundButton
import com.kdanmobile.reader.R
import kotlinx.android.synthetic.main.view_turn_page_mode_select.view.*

class TurnPageModeSelectView : ConstraintLayout {
    companion object {
        const val vertical = 0
        const val horizontal = 1
    }

    interface OnTurnPageModeChangedListener {
        fun onTurnPageModeChanged(direction: Int)
    }

    var onTurnPageModeChangedListener: OnTurnPageModeChangedListener? = null

    var onShowTurnPageBtnEnableChangedListener: CompoundButton.OnCheckedChangeListener? = null
        set(value) {
            field = value
            rowSwitchView_showTurnPageBtn?.onCheckedChangeListener = value
        }

    var direction: Int? = null
        set(value) {
            field = value
            when (value) {
                vertical -> radioBtn_turnPageMode_vertically.isChecked = true
                horizontal -> radioBtn_turnPageMode_horizontally.isChecked = true
            }
        }

    var isTurnPageBtnEnable: Boolean = false
        set(value) {
            field = value
            rowSwitchView_showTurnPageBtn?.isChecked = value
        }

    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_turn_page_mode_select, this)

        radioGroup_turnPageMode.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.radioBtn_turnPageMode_vertically -> onTurnPageModeChangedListener?.onTurnPageModeChanged(vertical)
                R.id.radioBtn_turnPageMode_horizontally -> onTurnPageModeChangedListener?.onTurnPageModeChanged(horizontal)
            }
        }
    }
}