package com.kdanmobile.reader.screen.model

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.content.Context
import com.kdanmobile.reader.screen.data.ViewerSettingSharePrefs

class SharePrefsModel(context: Context) {
    companion object {
        const val viewerSettingFileName = "viewer_setting"
    }

    val readMode: LiveData<Int>
        get() = mReadMode

    val isCropModeEnable: LiveData<Boolean>
        get() = mIsCropModeEnable

    val isShowStateBarEnable: LiveData<Boolean>
        get() = mIsShowStateBarEnable

    val turnPageDirection: LiveData<Int>
        get() = mTurnPageDirection

    val isShowTurnPageBtnEnable: LiveData<Boolean>
        get() = mIsShowTurnPageBtnEnable

    val isLockScreenSleepEnable: LiveData<Boolean>
        get() = mIsLockScreenSleepEnable

    val isShowCommentToolEnable: LiveData<Boolean>
        get() = mIsShowCommentToolEnable

    val isShowBOTAToolEnable: LiveData<Boolean>
        get() = mIsShowBOTAToolEnable

    private var mReadMode = MutableLiveData<Int>()

    private var mIsCropModeEnable = MutableLiveData<Boolean>()

    private var mIsShowStateBarEnable = MutableLiveData<Boolean>()

    private var mTurnPageDirection = MutableLiveData<Int>()

    private var mIsShowTurnPageBtnEnable = MutableLiveData<Boolean>()

    private var mIsLockScreenSleepEnable = MutableLiveData<Boolean>()

    private var mIsShowCommentToolEnable = MutableLiveData<Boolean>()

    private var mIsShowBOTAToolEnable = MutableLiveData<Boolean>()

    private val viewSettingSharePrefs = ViewerSettingSharePrefs(context, viewerSettingFileName).apply {
        mReadMode.value = this.getReadMode()
        mIsCropModeEnable.value = this.isCropModeEnable()
        mIsShowStateBarEnable.value = this.isShowStateBarEnable()
        mTurnPageDirection.value = this.getTurnPageDirection()
        mIsShowTurnPageBtnEnable.value = this.isShowTurnPageButtonEnable()
        mIsLockScreenSleepEnable.value = this.isLockScreenSleepEnable()
        mIsShowCommentToolEnable.value = this.isShowCommentToolEnable()
        mIsShowBOTAToolEnable.value = this.isShowBOTAToolEnable()
    }

    fun setReadMode(readMode: Int) {
        viewSettingSharePrefs.setReadMode(readMode)
        mReadMode.value = readMode
    }

    fun setCropModeEnable(enable: Boolean) {
        viewSettingSharePrefs.setCropModeEnable(enable)
        mIsCropModeEnable.value = enable
    }

    fun setShowStateBarEnable(enable: Boolean) {
        viewSettingSharePrefs.setShowStateBarEnable(enable)
        mIsShowStateBarEnable.value = enable
    }

    fun setTurnPageDirection(direction: Int) {
        viewSettingSharePrefs.setTurnPageDirection(direction)
        mTurnPageDirection.value = direction
    }

    fun setTurnPageButtonEnable(enable: Boolean) {
        viewSettingSharePrefs.setShowTurnPageButtonEnable(enable)
        mIsShowTurnPageBtnEnable.value = enable
    }

    fun setLockScreenSleepEnable(enable: Boolean) {
        viewSettingSharePrefs.setLockScreenSleepEnable(enable)
        mIsLockScreenSleepEnable.value = enable
    }

    fun setShowCommentToolEnable(enable: Boolean) {
        viewSettingSharePrefs.setShowCommentToolEnable(enable)
        mIsShowCommentToolEnable.value = enable
    }

    fun setShowBOTAToolEnable(enable: Boolean) {
        viewSettingSharePrefs.setShowBOTAToolEnable(enable)
        mIsShowBOTAToolEnable.value = enable
    }
}