package com.stand.standapp

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class DeveloperActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_developer)

        // 左上角退出
        findViewById<ImageView>(R.id.btn_close).setOnClickListener {
            finish()
        }

        // PTT 测试
        findViewById<LinearLayout>(R.id.item_ptt).setOnClickListener {
            startActivity(Intent(this, com.example.kingway.ptt.pttActivity::class.java))
        }

        // 打印测试
        findViewById<LinearLayout>(R.id.item_printer).setOnClickListener {
            startActivity(Intent(this, com.stand.standapp.printer.PrintTestActivity::class.java))
        }

        // 本地演示界面
        findViewById<LinearLayout>(R.id.item_demo_ui).setOnClickListener {
            val builder = androidx.appcompat.app.AlertDialog.Builder(this)
            builder.setTitle("本地演示登录")
            
            val layout = LinearLayout(this).apply {
                orientation = LinearLayout.VERTICAL
                setPadding(50, 40, 50, 10)
            }
            
            val etUser = EditText(this).apply {
                hint = "账号"
                setText("test007")
            }
            layout.addView(etUser)
            
            val etPwd = EditText(this).apply {
                hint = "密码"
                setText("123456")
                inputType = android.text.InputType.TYPE_CLASS_TEXT or android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD
            }
            layout.addView(etPwd)
            
            builder.setView(layout)
            builder.setPositiveButton("登录") { _, _ ->
                val username = etUser.text.toString().trim()
                val password = etPwd.text.toString().trim()
                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(this, "账号密码不能为空", Toast.LENGTH_SHORT).show()
                } else {
                    val intent = Intent(this, LoginActivity::class.java).apply {
                        putExtra("official_mode_login", false)
                        putExtra("extra_username", username)
                        putExtra("extra_password", password)
                    }
                    startActivity(intent)
                }
            }
            builder.setNegativeButton("取消", null)
            builder.show()
        }

        // 崩溃模拟
        findViewById<LinearLayout>(R.id.item_crash_test).setOnClickListener {
            androidx.appcompat.app.AlertDialog.Builder(this)
                .setTitle("确认崩溃测试？")
                .setMessage("应用将立即崩溃并尝试记录日志，确定继续吗？")
                .setPositiveButton("确定") { _, _ ->
                    throw RuntimeException("这是一次开发者手动触发的模拟崩溃测试")
                }
                .setNegativeButton("取消", null)
                .show()
        }

        // 加载调试信息
        loadDebugInfo()
    }

    private fun loadDebugInfo() {
        val tvInfo = findViewById<TextView>(R.id.tv_debug_info)
        val info = StringBuilder()
        info.append("设备型号: ${Build.MODEL}\n")
        info.append("系统版本: Android ${Build.VERSION.RELEASE} (SDK ${Build.VERSION.SDK_INT})\n")
        info.append("架构(ABI): ${Build.SUPPORTED_ABIS.joinToString(", ")}\n")
        info.append("运行模式: ${if (android.os.Process.is64Bit()) "64位" else "32位兼容模式"}\n")
        info.append("服务器地址: ${AppConfig.getServerUrl(this)}\n")
        info.append("串口路径: ${AppConfig.getSerialPort(this)}")
        
        tvInfo.text = info.toString()
    }
}
