#!/bin/bash

# 设置基础目录
BASE_DIR="."

# 创建5个库目录
LIB_NAMES=("nestedlib1" "nestedlib2" "nestedlib3" "nestedlib4" "nestedlib5")

# 为每个库创建目录结构
for lib_name in "${LIB_NAMES[@]}"; do
    LIB_DIR="$BASE_DIR/$lib_name"
    SRC_DIR="$LIB_DIR/src/commonMain/kotlin/com/dong/maxhap/$lib_name"
    
    # 创建目录结构
    mkdir -p "$SRC_DIR"
    
    echo "创建库目录: $LIB_DIR"
done

# 生成每个库的200个文件
FILES_PER_LIB=200
TOTAL_LIBS=5

for lib_index in $(seq 0 $((TOTAL_LIBS - 1))); do
    lib_name="${LIB_NAMES[$lib_index]}"
    LIB_DIR="$BASE_DIR/$lib_name"
    SRC_DIR="$LIB_DIR/src/commonMain/kotlin/com/dong/maxhap/$lib_name"
    
    echo "生成库 $lib_name 的文件..."
    
    # 生成200个嵌套页面文件
    for i in $(seq 1 $FILES_PER_LIB); do
        # 计算全局文件编号
        global_index=$((lib_index * FILES_PER_LIB + i))
        
        FILENAME="NestedPage${global_index}.kt"
        FILEPATH="$SRC_DIR/$FILENAME"
        
        # 生成函数名
        FUNCTION_NAME="NestedPage${global_index}"
        
        # 开始写入文件
        echo "package com.dong.maxhap.$lib_name" > "$FILEPATH"
        echo "" >> "$FILEPATH"
        echo "import androidx.compose.foundation.layout.*" >> "$FILEPATH"
        echo "import androidx.compose.material.*" >> "$FILEPATH"
        echo "import androidx.compose.runtime.*" >> "$FILEPATH"
        echo "import androidx.compose.ui.Alignment" >> "$FILEPATH"
        echo "import androidx.compose.ui.Modifier" >> "$FILEPATH"
        echo "import androidx.compose.ui.graphics.Color" >> "$FILEPATH"
        echo "import androidx.compose.ui.text.font.FontWeight" >> "$FILEPATH"
        echo "import androidx.compose.ui.unit.dp" >> "$FILEPATH"
        echo "import androidx.compose.ui.unit.sp" >> "$FILEPATH"
        echo "" >> "$FILEPATH"
        
        echo "@Composable" >> "$FILEPATH"
        echo "internal fun $FUNCTION_NAME() {" >> "$FILEPATH"
        
        # 添加状态管理（除了最后一个页面）
        if [ $global_index -lt $((TOTAL_LIBS * FILES_PER_LIB)) ]; then
            NEXT_PAGE_VAR="showNextPage"
            echo "    var $NEXT_PAGE_VAR by remember { mutableStateOf(false) }" >> "$FILEPATH"
            echo "" >> "$FILEPATH"
            echo "    if ($NEXT_PAGE_VAR) {" >> "$FILEPATH"
            if [ $i -lt $FILES_PER_LIB ]; then
                # 同一库内的调用
                echo "        NestedPage$((global_index + 1))()" >> "$FILEPATH"
            else
                # 跨库调用（如果不是最后一个库）
                if [ $lib_index -lt $((TOTAL_LIBS - 1)) ]; then
                    next_lib_name="${LIB_NAMES[$((lib_index + 1))]}"
                    echo "        com.dong.maxhap.$next_lib_name.NestedPage$((global_index + 1))()" >> "$FILEPATH"
                fi
            fi
            echo "        return" >> "$FILEPATH"
            echo "    }" >> "$FILEPATH"
            echo "" >> "$FILEPATH"
        fi
        
        # 主要内容布局
        echo "    Column(" >> "$FILEPATH"
        echo "        modifier = Modifier" >> "$FILEPATH"
        echo "            .fillMaxSize()" >> "$FILEPATH"
        echo "            .padding(16.dp)," >> "$FILEPATH"
        echo "        horizontalAlignment = Alignment.CenterHorizontally," >> "$FILEPATH"
        echo "        verticalArrangement = Arrangement.Center" >> "$FILEPATH"
        echo "    ) {" >> "$FILEPATH"
        echo "        Text(" >> "$FILEPATH"
        echo "            text = \"第${global_index}层页面 (${lib_name})\"," >> "$FILEPATH"
        echo "            fontSize = 24.sp," >> "$FILEPATH"
        echo "            fontWeight = FontWeight.Bold," >> "$FILEPATH"
        echo "            color = Color(0xFF$((lib_index * 50))0000)," >> "$FILEPATH"
        echo "            modifier = Modifier.padding(bottom = 20.dp)" >> "$FILEPATH"
        echo "        )" >> "$FILEPATH"
        
        # 生成一些文本内容
        for j in $(seq 1 10); do
            LINE_NUM=$(( (global_index - 1) * 10 + j ))
            RED=$(( (LINE_NUM * 17) % 256 ))
            GREEN=$(( (LINE_NUM * 31) % 256 ))
            BLUE=$(( (LINE_NUM * 47) % 256 ))
            echo "        Text(" >> "$FILEPATH"
            echo "            text = \"这是第${LINE_NUM}行内容\"," >> "$FILEPATH"
            echo "            fontSize = 14.sp," >> "$FILEPATH"
            echo "            color = Color($RED, $GREEN, $BLUE)," >> "$FILEPATH"
            echo "            modifier = Modifier.padding(vertical = 3.dp)" >> "$FILEPATH"
            echo "        )" >> "$FILEPATH"
        done
        
        # 添加导航按钮（除了最后一个页面）
        if [ $global_index -lt $((TOTAL_LIBS * FILES_PER_LIB)) ]; then
            echo "" >> "$FILEPATH"
            echo "        Spacer(modifier = Modifier.height(25.dp))" >> "$FILEPATH"
            echo "" >> "$FILEPATH"
            echo "        Button(" >> "$FILEPATH"
            echo "            onClick = { $NEXT_PAGE_VAR = true }," >> "$FILEPATH"
            echo "            modifier = Modifier" >> "$FILEPATH"
            echo "                .fillMaxWidth()" >> "$FILEPATH"
            echo "                .height(45.dp)," >> "$FILEPATH"
            echo "            colors = ButtonDefaults.buttonColors(" >> "$FILEPATH"
            echo "                backgroundColor = Color(0xFF2196F3)" >> "$FILEPATH"
            echo "            )" >> "$FILEPATH"
            echo "        ) {" >> "$FILEPATH"
            if [ $i -lt $FILES_PER_LIB ]; then
                echo "            Text(" >> "$FILEPATH"
                echo "                text = \"进入同库第$((global_index + 1))层\"," >> "$FILEPATH"
                echo "                fontSize = 16.sp," >> "$FILEPATH"
                echo "                fontWeight = FontWeight.Medium," >> "$FILEPATH"
                echo "                color = Color.White" >> "$FILEPATH"
                echo "            )" >> "$FILEPATH"
            else
                if [ $lib_index -lt $((TOTAL_LIBS - 1)) ]; then
                    next_lib_display="${LIB_NAMES[$((lib_index + 1))]}"
                    echo "            Text(" >> "$FILEPATH"
                    echo "                text = \"进入${next_lib_display}第$((global_index + 1))层\"," >> "$FILEPATH"
                    echo "                fontSize = 16.sp," >> "$FILEPATH"
                    echo "                fontWeight = FontWeight.Medium," >> "$FILEPATH"
                    echo "                color = Color.White" >> "$FILEPATH"
                    echo "            )" >> "$FILEPATH"
                else
                    echo "            Text(" >> "$FILEPATH"
                    echo "                text = \"已到达最深层\"," >> "$FILEPATH"
                    echo "                fontSize = 16.sp," >> "$FILEPATH"
                    echo "                fontWeight = FontWeight.Medium," >> "$FILEPATH"
                    echo "                color = Color.White" >> "$FILEPATH"
                    echo "            )" >> "$FILEPATH"
                fi
            fi
            echo "        }" >> "$FILEPATH"
        fi
        
        # 结束Column和函数
        echo "    }" >> "$FILEPATH"
        echo "}" >> "$FILEPATH"
        
        # 显示进度
        if [ $((i % 50)) -eq 0 ]; then
            echo "  已生成库 $lib_name 的 $i 个文件"
        fi
    done
done

# 为每个库创建build.gradle.kts文件
for lib_name in "${LIB_NAMES[@]}"; do
    LIB_DIR="$BASE_DIR/$lib_name"
    
    echo "plugins {" > "$LIB_DIR/build.gradle.kts"
    echo "    kotlin(\"multiplatform\")" >> "$LIB_DIR/build.gradle.kts"
    echo "    id(\"org.jetbrains.compose\")" >> "$LIB_DIR/build.gradle.kts"
    echo "}" >> "$LIB_DIR/build.gradle.kts"
    echo "" >> "$LIB_DIR/build.gradle.kts"
    echo "kotlin {" >> "$LIB_DIR/build.gradle.kts"
    echo "    jvm()" >> "$LIB_DIR/build.gradle.kts"
    echo "    androidTarget()" >> "$LIB_DIR/build.gradle.kts"
    echo "    iosX64()" >> "$LIB_DIR/build.gradle.kts"
    echo "    iosArm64()" >> "$LIB_DIR/build.gradle.kts"
    echo "    iosSimulatorArm64()" >> "$LIB_DIR/build.gradle.kts"
    echo "    ohosArm64()" >> "$LIB_DIR/build.gradle.kts"
    echo "" >> "$LIB_DIR/build.gradle.kts"
    echo "    sourceSets {" >> "$LIB_DIR/build.gradle.kts"
    echo "        val commonMain by getting {" >> "$LIB_DIR/build.gradle.kts"
    echo "            dependencies {" >> "$LIB_DIR/build.gradle.kts"
    echo "                implementation(compose.runtime)" >> "$LIB_DIR/build.gradle.kts"
    echo "                implementation(compose.foundation)" >> "$LIB_DIR/build.gradle.kts"
    echo "                implementation(compose.material)" >> "$LIB_DIR/build.gradle.kts"
    echo "            }" >> "$LIB_DIR/build.gradle.kts"
    echo "        }" >> "$LIB_DIR/build.gradle.kts"
    echo "    }" >> "$LIB_DIR/build.gradle.kts"
    echo "}" >> "$LIB_DIR/build.gradle.kts"
    
    echo "创建 $lib_name 的 build.gradle.kts"
done

# 更新settings.gradle.kts包含新库
SETTINGS_FILE="$BASE_DIR/settings.gradle.kts"
echo "" >> "$SETTINGS_FILE"
echo "// 嵌套页面库模块" >> "$SETTINGS_FILE"
for lib_name in "${LIB_NAMES[@]}"; do
    echo "include(\"$lib_name\")" >> "$SETTINGS_FILE"
done

echo ""
echo "成功生成5个嵌套页面库:"
echo "每个库包含200个文件，总共1000个嵌套页面"
echo "库列表: ${LIB_NAMES[*]}"
echo "请在composeApp的build.gradle.kts中添加依赖:"
echo ""
for lib_name in "${LIB_NAMES[@]}"; do
    echo "implementation(project(\":$lib_name\"))"
done