package tech.xiongweixp.touchaction

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.constraintlayout.widget.ConstraintLayout
import tech.xiongweixp.touchaction.ui.theme.TouchActionTheme
import java.net.HttpURLConnection
import java.net.URL

class MainActivity : ComponentActivity() {
    private var computerLayout: ConstraintLayout? = null
    private var lightLayout: ConstraintLayout? = null
    private var mainLayout: ConstraintLayout? = null
    private var computerTextView: TextView? = null
    private var compterCloseBtn: Button? = null
    private var lightTextView: TextView? = null
    private var lightCloseBtn: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.mainlayout)
        computerLayout = findViewById(R.id.computerLayout)
        lightLayout = findViewById(R.id.lightLayout)
        mainLayout = findViewById(R.id.mainLayout)
        computerTextView = findViewById(R.id.computerText)
        compterCloseBtn = findViewById(R.id.computerCloseButton)
        lightTextView = findViewById(R.id.lightText)
        lightCloseBtn = findViewById(R.id.lightCloseButton)

        compterCloseBtn!!.setOnClickListener {
            //退出APP
            finish()
        }

        lightCloseBtn!!.setOnClickListener {
            //退出APP
            finish()
        }
        enableEdgeToEdge()
        Log.i("MainActivity", "打开APP")
        Log.i("MainActivity", intent.data.toString())

    }



    private fun hassioRequest(webhookID: String) {
        Thread {
            try {
                val url = URL("http://192.168.31.45:8123/api/webhook/$webhookID")
                val conn = url.openConnection() as HttpURLConnection
                conn.requestMethod = "GET"
                conn.doInput = true
                conn.doOutput = false
                Log.i("MainActivity", "发送get请求：$webhookID" )
                conn.connect()
                val ret = conn.responseCode
                Log.i("MainActivity", "发送 $webhookID 请求成功，返回码：$ret")
                conn.disconnect()
                var device = 0;
                when (webhookID) {
                    "computer" -> {
                        showMessage(0, ret, "电脑开关操作成功")
                    }
                    "light" -> {
                        showMessage(1, ret, "灯开关切管成功")
                    }
                    else -> {
                        device = 2
                    }
                }

            }
            catch (e: Exception) {
                Log.i("MainActivity", "发送 $webhookID 请求失败")
                Log.i("MainActivity", e.toString())
                when(webhookID) {
                    "computer" -> {
                        showMessage(0, 0, "电脑操作失败")
                    }
                    "light" -> {
                        showMessage(1, 0, "灯操作失败")
                    }
                }
            }
        }.start()
    }

    private fun showMessage(device:Int, code:Int, msg:String) {
        // 在UI线程中更新UI
        runOnUiThread {
            // 显示提示信息
            when(device) {
                0 -> {
                    computerTextView!!.text = msg
                }
                1 -> {
                    lightTextView!!.text = msg
                }
                else -> {
                    mainLayout!!.visibility = ConstraintLayout.VISIBLE
                }
            }
        }
    }
    private fun dealAction() {
        Log.i("MainActivity", "感应到标签")
        var uri = intent.data
        if (uri!= null) {
            val action = uri!!.lastPathSegment
            Log.i("ActionActivity", "action: $action")
            when (action) {
                "computer" -> {
                    // 电脑
                    computerLayout!!.visibility = ConstraintLayout.VISIBLE
                    lightLayout!!.visibility = ConstraintLayout.INVISIBLE
                    mainLayout!!.visibility = ConstraintLayout.INVISIBLE
                    computerTextView!!.text = "正在操作电脑开关..."
                    hassioRequest("computer")
                }
                "light" -> {
                    // 灯
                    computerLayout!!.visibility = ConstraintLayout.INVISIBLE
                    lightLayout!!.visibility = ConstraintLayout.VISIBLE
                    mainLayout!!.visibility = ConstraintLayout.INVISIBLE
                    lightTextView!!.text = "正在操作灯开关..."
                    hassioRequest("light")
                }
                else -> {
                    // 主界面
                    computerLayout!!.visibility = ConstraintLayout.INVISIBLE
                    lightLayout!!.visibility = ConstraintLayout.INVISIBLE
                    mainLayout!!.visibility = ConstraintLayout.VISIBLE
                }
//                "save" -> {
//                    // 保存
//                }
//                "delete" -> {
//                    // 删除
//                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        Log.i("MainActivity", "resume打开APP")
        Log.i("MainActivity", intent.data.toString())
        dealAction()
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    TouchActionTheme {
        Greeting("Android")
    }
}

