package com.ez.ertc.setting

import android.os.Bundle
import android.preference.PreferenceManager
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.ActionBar
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.ez.basertc.RTCConstant
import com.ez.ertc.ConstantApp
import com.ez.ertc.R
import com.ez.ertc.base.BaseActivity
import kotlinx.android.synthetic.main.activity_settings.*
import kotlinx.android.synthetic.main.ard_agora_actionbar_with_back_btn.*

class SettingsActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        val ab: ActionBar? = supportActionBar
        if (ab != null) {
            ab.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
            var layout = View.inflate(this, R.layout.ard_agora_actionbar_with_back_btn, null)
            layout.findViewById<ImageView>(R.id.img_back).setOnClickListener {
                finish()
            }
            ab.setCustomView(layout, ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        }

        setupUI()

    }

    private fun setupUI() {
        ovc_page_title.setText(R.string.label_settings)
        settings_video_resolution.setHasFixedSize(true)
        settings_video_resolution.addItemDecoration(SettingsButtonDecoration())

        val pref = PreferenceManager.getDefaultSharedPreferences(this)
        val resolutionIdx = pref.getInt(ConstantApp.PrefManager.PREF_PROPERTY_VIDEO_ENC_RESOLUTION, ConstantApp.DEFAULT_VIDEO_ENC_RESOLUTION_IDX)

        val videoResolutionAdapter = VideoEncResolutionAdapter(this, resolutionIdx)
        videoResolutionAdapter.setHasStableIds(true)

        val layoutManager = GridLayoutManager(this, 3, LinearLayoutManager.VERTICAL, false)
        settings_video_resolution.layoutManager = layoutManager
        settings_video_resolution.adapter = videoResolutionAdapter

        val fps = pref.getInt(ConstantApp.PrefManager.PREF_PROPERTY_VIDEO_ENC_FPS, ConstantApp.DEFAULT_VIDEO_ENC_FPS)
        edit_fps.setText("$fps")

        val bps = pref.getInt(ConstantApp.PrefManager.PREF_PROPERTY_VIDEO_ENC_BPS, ConstantApp.DEFAULT_VIDEO_ENC_BPS)
        edit_bps.setText("$bps")

        val renderMode = pref.getInt(ConstantApp.PrefManager.PREF_PROPERTY_RENDER_MODE, RTCConstant.ERTC_VIDEO_RENDER_MODE_FILL)
        rg_render_mode.check(if (renderMode == RTCConstant.ERTC_VIDEO_RENDER_MODE_FILL) rb_fill.id else rb_fit.id)

        checkbox_bavlog.isChecked = pref.getBoolean(ConstantApp.ACTION_KEY_DEBUG_LOG, false)
        checkbox_hardDecode.isChecked = pref.getBoolean(ConstantApp.ACTION_KEY_HARDDECODE, false)
    }

    override fun onDestroy() {
        super.onDestroy()
        val pref = PreferenceManager.getDefaultSharedPreferences(this)
        pref.edit().putInt(ConstantApp.PrefManager.PREF_PROPERTY_VIDEO_ENC_FPS, edit_fps.text.toString().toInt()).apply()
        pref.edit().putInt(ConstantApp.PrefManager.PREF_PROPERTY_VIDEO_ENC_BPS, edit_bps.text.toString().toInt()).apply()

        var renderMode = if (rg_render_mode.checkedRadioButtonId == rb_fill.id) 0 else 1
        pref.edit().putInt(ConstantApp.PrefManager.PREF_PROPERTY_RENDER_MODE, renderMode).apply()
        pref.edit().putBoolean(ConstantApp.ACTION_KEY_DEBUG_LOG, checkbox_bavlog.isChecked).apply()
        pref.edit().putBoolean(ConstantApp.ACTION_KEY_HARDDECODE, checkbox_hardDecode.isChecked).apply()
    }
}