#!/bin/bash

# 设置目标目录
TARGET_DIR="composeApp/src/commonMain/kotlin/com/dong/maxhap/demos/test"

# 创建目录
mkdir -p "$TARGET_DIR"

# 设置总文件数量
TOTAL_FILES=200

# 从最后一个文件开始生成，实现嵌套结构
for i in $(seq $TOTAL_FILES -1 1); do
    FILENAME="NestedPage${i}.kt"
    FILEPATH="$TARGET_DIR/$FILENAME"
    
    # 生成函数名
    FUNCTION_NAME="NestedPage${i}"
    
    # 开始写入文件
    echo "package com.dong.maxhap.demos.test" > "$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 [ $i -lt $TOTAL_FILES ]; then
        NEXT_PAGE_VAR="showNextPage"
        echo "    var $NEXT_PAGE_VAR by remember { mutableStateOf(false) }" >> "$FILEPATH"
        echo "" >> "$FILEPATH"
        echo "    if ($NEXT_PAGE_VAR) {" >> "$FILEPATH"
        echo "        NestedPage$((i+1))()" >> "$FILEPATH"
        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 = \"第${i}层页面\"," >> "$FILEPATH"
    echo "            fontSize = 24.sp," >> "$FILEPATH"
    echo "            fontWeight = FontWeight.Bold," >> "$FILEPATH"
    echo "            color = Color(0xFF${i}0000)," >> "$FILEPATH"
    echo "            modifier = Modifier.padding(bottom = 20.dp)" >> "$FILEPATH"
    echo "        )" >> "$FILEPATH"
    
    # 生成一些文本内容
    for j in $(seq 1 15); do
        LINE_NUM=$(( (i - 1) * 15 + 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 = 16.sp," >> "$FILEPATH"
        echo "            color = Color($RED, $GREEN, $BLUE)," >> "$FILEPATH"
        echo "            modifier = Modifier.padding(vertical = 4.dp)" >> "$FILEPATH"
        echo "        )" >> "$FILEPATH"
    done
    
    # 添加导航按钮（除了最后一个页面）
    if [ $i -lt $TOTAL_FILES ]; then
        echo "" >> "$FILEPATH"
        echo "        Spacer(modifier = Modifier.height(30.dp))" >> "$FILEPATH"
        echo "" >> "$FILEPATH"
        echo "        Button(" >> "$FILEPATH"
        echo "            onClick = { $NEXT_PAGE_VAR = true }," >> "$FILEPATH"
        echo "            modifier = Modifier" >> "$FILEPATH"
        echo "                .fillMaxWidth()" >> "$FILEPATH"
        echo "                .height(50.dp)," >> "$FILEPATH"
        echo "            colors = ButtonDefaults.buttonColors(" >> "$FILEPATH"
        echo "                backgroundColor = Color(0xFF2196F3)" >> "$FILEPATH"
        echo "            )" >> "$FILEPATH"
        echo "        ) {" >> "$FILEPATH"
        echo "            Text(" >> "$FILEPATH"
        echo "                text = \"进入第$((i+1))层\"," >> "$FILEPATH"
        echo "                fontSize = 18.sp," >> "$FILEPATH"
        echo "                fontWeight = FontWeight.Medium," >> "$FILEPATH"
        echo "                color = Color.White" >> "$FILEPATH"
        echo "            )" >> "$FILEPATH"
        echo "        }" >> "$FILEPATH"
    else
        # 最后一层显示结束信息
        echo "" >> "$FILEPATH"
        echo "        Spacer(modifier = Modifier.height(30.dp))" >> "$FILEPATH"
        echo "        Text(" >> "$FILEPATH"
        echo "            text = \"已到达最深层（第${TOTAL_FILES}层）\"," >> "$FILEPATH"
        echo "            fontSize = 16.sp," >> "$FILEPATH"
        echo "            color = Color.Gray," >> "$FILEPATH"
        echo "            modifier = Modifier.padding(20.dp)" >> "$FILEPATH"
        echo "        )" >> "$FILEPATH"
    fi
    
    # 结束Column和函数
    echo "    }" >> "$FILEPATH"
    echo "}" >> "$FILEPATH"
    
    # 显示进度
    if [ $((TOTAL_FILES - i + 1 % 100)) -eq 0 ]; then
        echo "已生成 $((TOTAL_FILES - i + 1)) 个文件"
    fi
done

# 创建主入口文件
MAIN_FILE="$TARGET_DIR/NestedPagesMain.kt"
echo "package com.dong.maxhap.demos.test" > "$MAIN_FILE"
echo "" >> "$MAIN_FILE"
echo "import androidx.compose.foundation.layout.*" >> "$MAIN_FILE"
echo "import androidx.compose.material.*" >> "$MAIN_FILE"
echo "import androidx.compose.runtime.*" >> "$MAIN_FILE"
echo "import androidx.compose.ui.Alignment" >> "$MAIN_FILE"
echo "import androidx.compose.ui.Modifier" >> "$MAIN_FILE"
echo "import androidx.compose.ui.graphics.Color" >> "$MAIN_FILE"
echo "import androidx.compose.ui.text.font.FontWeight" >> "$MAIN_FILE"
echo "import androidx.compose.ui.unit.dp" >> "$MAIN_FILE"
echo "import androidx.compose.ui.unit.sp" >> "$MAIN_FILE"
echo "" >> "$MAIN_FILE"
echo "@Composable" >> "$MAIN_FILE"
echo "internal fun NestedPagesMain() {" >> "$MAIN_FILE"
echo "    var showFirstPage by remember { mutableStateOf(false) }" >> "$MAIN_FILE"
echo "" >> "$MAIN_FILE"
echo "    if (showFirstPage) {" >> "$MAIN_FILE"
echo "        NestedPage1()" >> "$MAIN_FILE"
echo "        return" >> "$MAIN_FILE"
echo "    }" >> "$MAIN_FILE"
echo "" >> "$MAIN_FILE"
echo "    Column(" >> "$MAIN_FILE"
echo "        modifier = Modifier" >> "$MAIN_FILE"
echo "            .fillMaxSize()" >> "$MAIN_FILE"
echo "            .padding(16.dp)," >> "$MAIN_FILE"
echo "        horizontalAlignment = Alignment.CenterHorizontally," >> "$MAIN_FILE"
echo "        verticalArrangement = Arrangement.Center" >> "$MAIN_FILE"
echo "    ) {" >> "$MAIN_FILE"
echo "        Text(" >> "$MAIN_FILE"
echo "            text = \"嵌套页面演示\"," >> "$MAIN_FILE"
echo "            fontSize = 28.sp," >> "$MAIN_FILE"
echo "            fontWeight = FontWeight.Bold," >> "$MAIN_FILE"
echo "            color = Color(0xFF2196F3)," >> "$MAIN_FILE"
echo "            modifier = Modifier.padding(bottom = 30.dp)" >> "$MAIN_FILE"
echo "        )" >> "$MAIN_FILE"
echo "" >> "$MAIN_FILE"
echo "        Text(" >> "$MAIN_FILE"
echo "            text = \"共包含 $TOTAL_FILES 层嵌套页面\"," >> "$MAIN_FILE"
echo "            fontSize = 16.sp," >> "$MAIN_FILE"
echo "            modifier = Modifier.padding(bottom = 20.dp)" >> "$MAIN_FILE"
echo "        )" >> "$MAIN_FILE"
echo "" >> "$MAIN_FILE"
echo "        Button(" >> "$MAIN_FILE"
echo "            onClick = { showFirstPage = true }," >> "$MAIN_FILE"
echo "            modifier = Modifier" >> "$MAIN_FILE"
echo "                .fillMaxWidth()" >> "$MAIN_FILE"
echo "                .height(50.dp)," >> "$MAIN_FILE"
echo "            colors = ButtonDefaults.buttonColors(" >> "$MAIN_FILE"
echo "                backgroundColor = Color(0xFF4CAF50)" >> "$MAIN_FILE"
echo "            )" >> "$MAIN_FILE"
echo "        ) {" >> "$MAIN_FILE"
echo "            Text(" >> "$MAIN_FILE"
echo "                text = \"开始嵌套之旅\"," >> "$MAIN_FILE"
echo "                fontSize = 18.sp," >> "$MAIN_FILE"
echo "                fontWeight = FontWeight.Medium," >> "$MAIN_FILE"
echo "                color = Color.White" >> "$MAIN_FILE"
echo "            )" >> "$MAIN_FILE"
echo "        }" >> "$MAIN_FILE"
echo "    }" >> "$MAIN_FILE"
echo "}" >> "$MAIN_FILE"

echo "成功生成 $TOTAL_FILES 个嵌套页面文件到 $TARGET_DIR 目录"
echo "主入口函数: NestedPagesMain()"