package com.narutohuo.xindazhou.share.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.narutohuo.xindazhou.share.factory.ShareServiceFactory
import com.narutohuo.xindazhou.core.log.ILog
import com.narutohuo.xindazhou.share.model.ShareContent
import com.narutohuo.xindazhou.share.model.SharePlatform
import com.narutohuo.xindazhou.share.model.ShareResponse

/**
 * 分享弹窗（底部弹出）
 * 
 * 显示所有可用的分享平台，用户选择后执行分享
 */
class ShareDialogFragment : BottomSheetDialogFragment() {
    
    private var shareService: com.narutohuo.xindazhou.share.impl.ShareServiceImpl? = null
    
    companion object {
        /**
         * 创建分享弹窗实例
         * 
         * @param shareService 分享服务实例（用于执行分享）
         */
        @JvmStatic
        fun newInstance(
            shareService: com.narutohuo.xindazhou.share.impl.ShareServiceImpl
        ): ShareDialogFragment {
            val fragment = ShareDialogFragment()
            fragment.shareService = shareService
            return fragment
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // 创建简单的线性布局
        val rootLayout = LinearLayout(requireContext()).apply {
            orientation = LinearLayout.VERTICAL
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            setPadding(32, 32, 32, 32)
        }
        
        // 标题
        val titleView = TextView(requireContext()).apply {
            text = "分享到"
            textSize = 18f
            setPadding(0, 0, 0, 24)
        }
        rootLayout.addView(titleView)
        
        // 创建平台网格布局
        val gridLayout = LinearLayout(requireContext()).apply {
            orientation = LinearLayout.VERTICAL
        }
        
        // 可用的分享平台列表
        val platforms = listOf(
            SharePlatform.WECHAT,
            SharePlatform.WECHAT_MOMENTS,
            SharePlatform.QQ,
            SharePlatform.QZONE,
            SharePlatform.WEIBO,
            SharePlatform.SYSTEM
        )
        
        // 每行4个平台
        val rowCount = (platforms.size + 3) / 4
        for (row in 0 until rowCount) {
            val rowLayout = LinearLayout(requireContext()).apply {
                orientation = LinearLayout.HORIZONTAL
                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
            }
            
            for (col in 0 until 4) {
                val index = row * 4 + col
                if (index < platforms.size) {
                    val platform = platforms[index]
                    val platformView = createPlatformView(platform)
                    rowLayout.addView(platformView)
                }
            }
            
            gridLayout.addView(rowLayout)
        }
        
        rootLayout.addView(gridLayout)
        
        // 取消按钮
        val cancelView = TextView(requireContext()).apply {
            text = "取消"
            textSize = 16f
            setPadding(0, 24, 0, 0)
            setOnClickListener { dismiss() }
        }
        rootLayout.addView(cancelView)
        
        return rootLayout
    }
    
    /**
     * 创建平台视图（图标 + 文字）
     */
    private fun createPlatformView(platform: SharePlatform): View {
        val container = LinearLayout(requireContext()).apply {
            orientation = LinearLayout.VERTICAL
            layoutParams = LinearLayout.LayoutParams(
                0,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                1f
            ).apply {
                setMargins(8, 8, 8, 8)
            }
            gravity = android.view.Gravity.CENTER
            setOnClickListener { onPlatformSelected(platform) }
        }
        
        // 图标（暂时用文字代替，后续可以替换为图标）
        val iconView = TextView(requireContext()).apply {
            text = getPlatformIcon(platform)
            textSize = 32f
            gravity = android.view.Gravity.CENTER
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        }
        container.addView(iconView)
        
        // 平台名称
        val nameView = TextView(requireContext()).apply {
            text = getPlatformName(platform)
            textSize = 12f
            gravity = android.view.Gravity.CENTER
            setPadding(0, 8, 0, 0)
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        }
        container.addView(nameView)
        
        return container
    }
    
    private fun getPlatformIcon(platform: SharePlatform): String {
        return when (platform) {
            SharePlatform.WECHAT -> "💬"
            SharePlatform.WECHAT_MOMENTS -> "📱"
            SharePlatform.QQ -> "🐧"
            SharePlatform.QZONE -> "🌐"
            SharePlatform.WEIBO -> "📢"
            SharePlatform.DOUYIN -> "🎵"
            SharePlatform.XIAOHONGSHU -> "📕"
            SharePlatform.KUAISHOU -> "⚡"
            SharePlatform.SYSTEM -> "📤"
        }
    }
    
    private fun getPlatformName(platform: SharePlatform): String {
        return when (platform) {
            SharePlatform.WECHAT -> "微信"
            SharePlatform.WECHAT_MOMENTS -> "朋友圈"
            SharePlatform.QQ -> "QQ"
            SharePlatform.QZONE -> "QQ空间"
            SharePlatform.WEIBO -> "微博"
            SharePlatform.DOUYIN -> "抖音"
            SharePlatform.XIAOHONGSHU -> "小红书"
            SharePlatform.KUAISHOU -> "快手"
            SharePlatform.SYSTEM -> "更多"
        }
    }
    
    private fun onPlatformSelected(platform: SharePlatform) {
        ILog.d("ShareDialogFragment", "onPlatformSelected: $platform")
        dismiss()
        
        // 调用分享服务执行分享
        shareService?.executeShare(requireActivity(), platform)
    }
}

