package com.mime.dualscreenview.activity

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import com.mime.dualscreenview.R
import com.mime.dualscreenview.common.PairArray
import com.mime.dualscreenview.common.colorz
import com.mime.dualscreenview.common.getIndex
import com.mime.dualscreenview.common.typesfacez
import com.mime.dualscreenview.data.HistoryManager
import com.mime.dualscreenview.data.model.ReaderConfig
import com.mime.dualscreenview.databinding.SettingsBinding
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query

class Settings : Base() {

    lateinit var binding : SettingsBinding
    var readerConfig : ReaderConfig? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.settings)
//        setContentView(R.layout.settings)
        var realm = HistoryManager.openRealm()
        realm.writeBlocking {
            this.query<ReaderConfig>()?.find()?.let {
                if (it.size > 0) {
                    readerConfig = copyFromRealm(it.first())
                } else {
                    readerConfig = ReaderConfig()
                }
            }

        }
        realm.close()
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onResume() {
        super.onResume()
        binding.textSize.displayFormat = "글자 크기 : %d"
        binding.textSize.value = readerConfig?.textSize ?:  14
        binding.textSize.mValueChange = {
            binding.preview.textSize = it.toFloat() ;
            if (readerConfig?.textSize != it) {
                readerConfig?.textSize = it
                configSave()
            }
        }


        binding.pagePadding.displayFormat = "페이지 여백 :  %d"
        binding.pagePadding.value = readerConfig?.padding ?: 5
        binding.pagePadding.mValueChange = {
            binding.preview.setPadding(it,it,it,it) ;
            if (readerConfig?.padding != it) {
                readerConfig?.padding = it
                configSave()
            }
        }


        binding.letterSpace.displayFormat = "자간 : %d"
        binding.letterSpace.value = readerConfig?.letterSpace ?: 1
        binding.letterSpace.mValueChange = {
            binding.preview.letterSpacing = it.times(0.01).toFloat() ;
            if (readerConfig?.letterSpace != it) {
                readerConfig?.letterSpace = it
                configSave()
            }
        }


        binding.lineSpace.displayFormat = "행간 : %d"
        binding.lineSpace.value = readerConfig?.lineSpace ?: 1
        binding.lineSpace.mValueChange = {
            binding.preview.setLineSpacing(1f, 1f.plus(it.times(0.01f))) ;
            if (readerConfig?.lineSpace != it) {
                readerConfig?.lineSpace = it
                configSave()
            }
        }



        binding.pageTypesface.displayFormat = "폰트 : %s"
        binding.pageTypesface.titleArray = typesfacez.map { it.first }.toTypedArray()
        binding.pageTypesface.value = getIndex(typesfacez as PairArray<Any>,readerConfig?.font ?: "")
        binding.pageTypesface.mValueChange = {
            val pair = typesfacez.get(it)
            binding.preview.setTypeface(resources.getFont(pair.second))
            if (readerConfig?.font != pair.first) {
                readerConfig?.font = pair.first ?: ""
                configSave()
            }
        }

        binding.pageStyle.displayFormat = "스타일 : %s"
        binding.pageStyle.titleArray = colorz.map { it.first }.toTypedArray()
        binding.pageStyle.value =  readerConfig?.style ?: 0
        binding.pageStyle.mValueChange = {
            val pair = colorz.get(it)
            binding.preview.setBackgroundColor(Color.parseColor(pair.second.last()))
            binding.preview.setTextColor(Color.parseColor(pair.second.first()))
            if (readerConfig?.style != it) {
                readerConfig?.style = it ?: 0
                configSave()
            }
        }

    }



    fun configSave() {
        var realm = HistoryManager.openRealm()
        realm.writeBlocking {
            copyToRealm(readerConfig!!, UpdatePolicy.ALL)
        }
        realm.close()
    }
}