// chapter-generator.page.ts
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms'; // 导入 FormsModule
import { IonRouterOutlet } from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { chevronForward } from 'ionicons/icons';
import { IonicModule, ModalController } from '@ionic/angular';
import { CommonModule } from '@angular/common';
import { AiExpandModalComponent } from '../ai-expand-modal/ai-expand-modal.component';
import { AiPolishModalComponent } from '../ai-polish-modal/ai-polish-modal.component';
import { AiContinueModalComponent } from '../ai-continue-modal/ai-continue-modal.component';
import { OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
addIcons({ chevronForward });

@Component({
  selector: 'app-chapter-generator',
  templateUrl: './chapter-generator.page.html',
  styleUrls: ['./chapter-generator.page.scss'],
  standalone: true,
  imports: [
    FormsModule,
    IonRouterOutlet,
    CommonModule,
    IonicModule,
    AiExpandModalComponent,
    AiPolishModalComponent,
    AiContinueModalComponent
    ]
})
export class ChapterGeneratorPage implements OnInit {
  chapters = [
    { title: 'Chapter 1', content: '这是第一章的内容。' },
    // 其他章节...
  ];
  isSideShow: boolean = true;
  selectedChapterIndex: number | null = null;
  selectedChapterTitle: string = '';
  selectedChapterContent: string = '';

  title: string = '';
  description: string = '';

  constructor(private modalCtrl: ModalController, private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      this.title = params['title'];
      this.description = params['description'];
      console.log('Received Title:', this.title);
      console.log('Received Description:', this.description);
    });
  }
  async openAiExpandModal() {
    const modal = await this.modalCtrl.create({
      component: AiExpandModalComponent,
      componentProps: {
        onInsertText: (text: string) => this.insertTextIntoChapterEnd(text)
      }
    });
    return await modal.present();
  }

  async openAiPolishModal() {
    const modal = await this.modalCtrl.create({
      component: AiPolishModalComponent,
      componentProps: {
        onInsertText: (text: string) => this.insertTextIntoChapterEnd(text)
      }
    });
    return await modal.present();
  }

  async openAiContinueModal() {
    const modal = await this.modalCtrl.create({
      component: AiContinueModalComponent,
      componentProps: {
        onInsertText: (text: string) => this.insertTextIntoChapterEnd(text)
      }
    });
    return await modal.present();
  }

  toggleSide() {
    this.isSideShow = !this.isSideShow;
  }

  addChapter() {
    const newChapter = { title: `Chapter ${this.chapters.length + 1}`, content: '' };
    this.chapters.push(newChapter);
    this.selectChapter(this.chapters.length - 1); // 自动编辑新添加的章节
  }

  deleteChapter(index: number) {
    if (index === this.selectedChapterIndex) {
      this.selectedChapterIndex = null;
      this.selectedChapterTitle = '';
      this.selectedChapterContent = '';
    }
    this.chapters.splice(index, 1);
  }

  selectChapter(index: number) {
    this.editChapter(index);
    this.isSideShow = false; // 隐藏侧边栏
  }

  editChapter(index: number) {
    this.selectedChapterIndex = index;
    this.selectedChapterTitle = this.chapters[index].title;
    this.selectedChapterContent = this.chapters[index].content; // 初始化内容为当前章节的内容
  }

  saveChapter() {
    if (this.selectedChapterIndex !== null) {
      this.chapters[this.selectedChapterIndex].title = this.selectedChapterTitle;
      this.chapters[this.selectedChapterIndex].content = this.selectedChapterContent;
      console.log('章节内容已保存:', this.selectedChapterContent);
      this.selectedChapterIndex = null;
      this.selectedChapterTitle = '';
      this.selectedChapterContent = '';
      this.isSideShow = true; // 显示侧边栏
    }
  }

  insertTextIntoChapterEnd(text: string) {
    if (this.selectedChapterIndex !== null) {
      this.chapters[this.selectedChapterIndex].content += text;
      this.selectedChapterContent += text; // 更新显示的内容
    }
  }
}