package com.narutohuo.xindazhou.community.ui

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.narutohuo.xindazhou.R
import com.narutohuo.xindazhou.common.ui.BaseActivity
import com.narutohuo.xindazhou.databinding.ActivityCommunityBinding
import com.narutohuo.xindazhou.qrcode.factory.QRCodeManagerFactory
import com.narutohuo.xindazhou.service.ui.ServiceActivity
import com.narutohuo.xindazhou.shop.ui.ShopActivity
import com.narutohuo.xindazhou.user.ui.UserActivity
import com.narutohuo.xindazhou.vehicle.ui.VehicleActivity

/**
 * 社区 Activity
 * 
 * 架构A：每个页面一个Activity
 * 显示社区相关功能
 */
class CommunityActivity : BaseActivity<ActivityCommunityBinding>() {
    
    private val qrCodeManager = QRCodeManagerFactory.getInstance()
    
    override fun getViewBinding(): ActivityCommunityBinding {
        return ActivityCommunityBinding.inflate(layoutInflater)
    }
    
    override fun initView() {
        // 创建布局
        val layout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            gravity = android.view.Gravity.CENTER
            setPadding(32, 32, 32, 32)
        }

        // 标题
        val textView = TextView(this).apply {
            text = "社区页面"
            textSize = 18f
            gravity = android.view.Gravity.CENTER
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                setMargins(0, 0, 0, 32)
            }
        }
        layout.addView(textView)

        // 扫码按钮
        val scanButton = Button(this).apply {
            text = "扫码"
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            setOnClickListener {
                startScanQRCode()
            }
        }
        layout.addView(scanButton)
        
        // 将内容添加到布局中
        binding.contentContainer.removeAllViews()
        binding.contentContainer.addView(layout)
        
        // 设置底部导航栏
        setupBottomNavigation()
    }
    
    /**
     * 开始扫码
     */
    private fun startScanQRCode() {
        qrCodeManager.scanQRCode(this) { response ->
            if (response.success) {
                val qrCodeContent = response.data
                Toast.makeText(
                    this,
                    "扫码成功：$qrCodeContent",
                    Toast.LENGTH_SHORT
                ).show()
                // TODO: 处理扫码结果
            } else {
                val error = response.errorMessage
                if (error != null) {
                    Toast.makeText(
                        this,
                        "扫码失败：$error",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }
    
    private fun setupBottomNavigation() {
        binding.bottomNavView.selectedItemId = R.id.communityFragment
        binding.bottomNavView.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.vehicleFragment -> {
                    startActivity(Intent(this, VehicleActivity::class.java))
                    finish()
                    true
                }
                R.id.communityFragment -> {
                    // 当前已在社区模块，不需要跳转
                    true
                }
                R.id.serviceFragment -> {
                    startActivity(Intent(this, ServiceActivity::class.java))
                    finish()
                    true
                }
                R.id.shopFragment -> {
                    startActivity(Intent(this, ShopActivity::class.java))
                    finish()
                    true
                }
                R.id.userFragment -> {
                    startActivity(Intent(this, UserActivity::class.java))
                    finish()
                    true
                }
                else -> false
            }
        }
    }
}

