#!/bin/bash

# 为所有NestedPage文件添加@Stable元素的简化脚本

BASE_DIR="/Users/dongsq/TestDemo/IR013/TestProject/MaxHap"
LIB_NAMES=("nestedlib1" "nestedlib2" "nestedlib3" "nestedlib4" "nestedlib5")

echo "开始为NestedPage文件添加@Stable元素..."

# 重新生成所有文件（备份原始文件）
for lib_name in "${LIB_NAMES[@]}"; do
    LIB_DIR="$BASE_DIR/$lib_name/src/commonMain/kotlin/com/dong/maxhap/$lib_name"
    
    if [ ! -d "$LIB_DIR" ]; then
        echo "目录不存在: $LIB_DIR"
        continue
    fi
    
    echo "处理库: $lib_name"
    
    # 获取该库中的所有NestedPage文件数量
    TOTAL_FILES=$(find "$LIB_DIR" -name "NestedPage*.kt" | wc -l)
    echo "找到 $TOTAL_FILES 个文件"
    
    # 重新生成每个文件
    for ((i=1; i<=200; i++)); do
        FILE_PATH="$LIB_DIR/NestedPage$(( (${lib_name: -1} - 1) * 200 + i )).kt"
        GLOBAL_INDEX=$(( (${lib_name: -1} - 1) * 200 + i ))
        
        if [ ! -f "$FILE_PATH" ]; then
            continue
        fi
        
        # 读取原文件内容（除了imports部分）
        ORIGINAL_CONTENT=$(sed -n '/^import /,/^@Composable/p' "$FILE_PATH" | head -n -1)
        
        # 生成8个@Stable数据类
        STABLE_CLASSES=""
        for j in {1..8}; do
            STABLE_CLASSES+="@Stable
data class StableData${GLOBAL_INDEX}_${j}(
    val id: Int = $((GLOBAL_INDEX * 1000 + j)),
    val name: String = \"Stable_${GLOBAL_INDEX}_${j}\",
    val value: Double = ${GLOBAL_INDEX}.${j},
    val isActive: Boolean = true,
    val timestamp: Long = System.currentTimeMillis(),
    val tags: List<String> = listOf(\"tag1\", \"tag2\", \"tag3\"),
    val metadata: Map<String, String> = mapOf(\"key1\" to \"value1\", \"key2\" to \"value2\"),
    val nestedData: NestedStableData${GLOBAL_INDEX}_${j}? = null
)

@Stable
data class NestedStableData${GLOBAL_INDEX}_${j}(
    val subId: Int = $((GLOBAL_INDEX * 100 + j)),
    val description: String = \"Nested stable data for page ${GLOBAL_INDEX}, item ${j}\",
    val count: Int = ${j}00,
    val isEnabled: Boolean = $((j % 2 == 0))
)

"
        done
        
        # 重新构建文件
        {
            echo "package com.dong.maxhap.$lib_name"
            echo ""
            echo "import androidx.compose.foundation.layout.*"
            echo "import androidx.compose.material.*"
            echo "import androidx.compose.runtime.*"
            echo "import androidx.compose.ui.Alignment"
            echo "import androidx.compose.ui.Modifier"
            echo "import androidx.compose.ui.graphics.Color"
            echo "import androidx.compose.ui.text.font.FontWeight"
            echo "import androidx.compose.ui.unit.dp"
            echo "import androidx.compose.ui.unit.sp"
            echo "import androidx.compose.runtime.Stable"
            echo ""
            echo "$STABLE_CLASSES"
            sed -n '/^@Composable/,$p' "$FILE_PATH"
        } > "${FILE_PATH}.new"
        
        mv "${FILE_PATH}.new" "$FILE_PATH"
        
        # 显示进度
        if [ $((i % 50)) -eq 0 ]; then
            echo "  已处理 ${lib_name} 的第 $i 个页面"
        fi
    done
done

echo "完成！所有NestedPage文件已添加@Stable元素。"
echo "每个页面现在包含8个@Stable数据类。"