package com.narutohuo.xindazhou.shop.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.core.log.ILog
import com.narutohuo.xindazhou.databinding.ActivityShopBinding
import com.narutohuo.xindazhou.community.ui.CommunityActivity
import com.narutohuo.xindazhou.service.ui.ServiceActivity
import com.narutohuo.xindazhou.share.ShareKit
import com.narutohuo.xindazhou.share.model.ShareContent
import com.narutohuo.xindazhou.user.ui.UserActivity
import com.narutohuo.xindazhou.vehicle.ui.VehicleActivity

/**
 * 商城 Activity
 * 
 * 架构A：每个页面一个Activity
 * 显示商城相关功能
 */
class ShopActivity : BaseActivity<ActivityShopBinding>() {
    
    private val tag = "ShopActivity"
    
    override fun getViewBinding(): ActivityShopBinding {
        return ActivityShopBinding.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 shareButton = Button(this).apply {
            text = "分享"
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            setOnClickListener {
                shareContent()
            }
        }
        layout.addView(shareButton)
        
        // 将内容添加到布局中
        binding.contentContainer.removeAllViews()
        binding.contentContainer.addView(layout)
        
        // 设置底部导航栏
        setupBottomNavigation()
    }
    
    /**
     * 分享内容
     */
    private fun shareContent() {
        try {
            val shareService = ShareKit.getInstance()
            
            // 创建分享内容（不指定平台，会弹出分享弹窗让用户选择）
            val shareContent = ShareContent.builder()
                .setTitle("新大洲本田商城")
                .setDescription("欢迎来到新大洲本田商城，这里有丰富的摩托车配件和用品！")
                .setUrl("https://www.xindazhou.com/shop")  // 示例链接
                // .setImageUrl("https://example.com/image.jpg")  // 可选：分享图片
                .build()
            
            // 调用分享（不指定平台，会显示分享弹窗）
            shareService.share(this, shareContent) { response ->
                if (response.success) {
                    val platformName = response.data?.name ?: "未知平台"
                    Toast.makeText(this, "分享成功：$platformName", Toast.LENGTH_SHORT).show()
                    ILog.d(tag, "分享成功：$platformName")
                } else {
                    val errorMsg = response.errorMessage ?: "分享失败"
                    Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show()
                    ILog.e(tag, "分享失败：$errorMsg")
                }
            }
        } catch (e: Exception) {
            ILog.e(tag, "分享功能异常", e)
            Toast.makeText(this, "分享功能异常：${e.message}", Toast.LENGTH_SHORT).show()
        }
    }
    
    private fun setupBottomNavigation() {
        binding.bottomNavView.selectedItemId = R.id.shopFragment
        binding.bottomNavView.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.vehicleFragment -> {
                    startActivity(Intent(this, VehicleActivity::class.java))
                    finish()
                    true
                }
                R.id.communityFragment -> {
                    startActivity(Intent(this, CommunityActivity::class.java))
                    finish()
                    true
                }
                R.id.serviceFragment -> {
                    startActivity(Intent(this, ServiceActivity::class.java))
                    finish()
                    true
                }
                R.id.shopFragment -> {
                    // 当前已在商城模块，不需要跳转
                    true
                }
                R.id.userFragment -> {
                    startActivity(Intent(this, UserActivity::class.java))
                    finish()
                    true
                }
                else -> false
            }
        }
    }
}

