package com.jld.vod.view

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.KeyEvent
import android.view.MenuItem
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.jld.vod.R
import com.jld.vod.config.Config
import com.jld.vod.model.event.ReceiveEvent
import com.jld.vod.service.SocketService
import com.jld.vod.socket.SendSocketManager
import com.jld.vod.utils.LogUtils
import org.greenrobot.eventbus.EventBus
import org.json.JSONException
import org.json.JSONObject

class SettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // hide titlebar of application
        // must be before setting the layout
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hide statusbar of Android
        // could also be done later
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.settings_activity)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings, SettingsFragment())
            .commit()
        supportActionBar?.setDisplayHomeAsUpEnabled(true)


        val jsonObject = JSONObject()
        try {
            jsonObject.put(Config.TYPE, "CLOSE")
            jsonObject.put(Config.MSG, "false")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        //关闭udp接收
        EventBus.getDefault().post(ReceiveEvent(jsonObject.toString()))

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home)
        {
            val mIntent = Intent(this, SplashActivity::class.java)
            mIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(mIntent)
            finish()
        }
        return super.onOptionsItemSelected(item)
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            val mIntent = Intent(this, SplashActivity::class.java)
            mIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(mIntent)
            finish()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    class SettingsFragment : PreferenceFragmentCompat() {
        private var sendSocketManager: SendSocketManager? = null

        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

            sendSocketManager = SendSocketManager.getInstance(activity)
            sendSocketManager!!.startUdpConnection()

            setPreferencesFromResource(R.xml.root_preferences, rootKey)
            val wifi_settings = findPreference<Preference>("wifi_settings")
            val seat_settings = findPreference<Preference>("seat_settings")

            val ads_control = findPreference<Preference>("ads_control")
            val screen_control = findPreference<Preference>("screen_control")
            wifi_settings!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
                var i: Intent? = Intent()
                i = Intent(Settings.ACTION_WIFI_SETTINGS)
                startActivity(i)

                false
            }
            //设置座位号
            seat_settings!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
                val intent = Intent(activity,SettingActivity::class.java)
                startActivity(intent)
                false
            }

            //一键广告播放
            ads_control!!.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
                LogUtils.logD("preference"+preference+"newValue"+newValue)
                sendSocketManager!!.sendMsg(Config.ADS, newValue.toString())
                true
            }

            //一键熄屏
            screen_control!!.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
                LogUtils.logD("preference"+preference+"newValue"+newValue)
                sendSocketManager!!.sendMsg(Config.SCREEN, newValue.toString())
                true
            }

        }
    }
}