import { Injectable } from '@angular/core';
import { Diary } from '../modals/diary.modal';
import { AlertController } from '@ionic/angular'; // 添加这行

@Injectable({
  providedIn: 'root'
})
export class DiaryService {
  private storageKey = 'diaries';
    modalCtrl: any;
  
    //alertCtrl: any;
    //storage: any;

  constructor(private alertCtrl: AlertController) {}

  async getDiaries(): Promise<Diary[]> {
    const data = localStorage.getItem(this.storageKey);
    return data ? JSON.parse(data) : [];
  }

  async addDiary(diary: Diary): Promise<void> {
    const diaries = await this.getDiaries();
    diaries.push(diary);
    localStorage.setItem(this.storageKey, JSON.stringify(diaries));
  }

  async updateDiary(updatedDiary: Diary): Promise<void> {
    const diaries = await this.getDiaries();
    const index = diaries.findIndex(d => d.id === updatedDiary.id);
    if (index !== -1) {
      diaries[index] = updatedDiary;
      localStorage.setItem(this.storageKey, JSON.stringify(diaries));
    }
  }

//   async deleteDiary(id: number): Promise<void> {
//     const diaries = await this.getDiaries();
//     const updatedDiaries = diaries.filter(d => d.id !== id);
//     //await this.storage.set('diaries', updatedDiaries);
//     localStorage.setItem(this.storageKey, JSON.stringify(updatedDiaries));
//   }

//   async deleteDiary(id: number): Promise<boolean> {
//     return new Promise<boolean>(async (resolve) => {
//       const alert = await this.alertCtrl.create({
//         header: '确认删除',
//         message: '确定要删除这篇日记吗？',
//         buttons: [
//           {
//             text: '取消',
//             role: 'cancel',
//             handler: () => resolve(false)
//           },
//           {
//             text: '删除',
//             handler: async () => {
//               const diaries = await this.getDiaries();
//               const updatedDiaries = diaries.filter(d => d.id !== id);
//               localStorage.setItem(this.storageKey, JSON.stringify(updatedDiaries));
//               resolve(true);
//             }
//           }
//         ]
//       });
      
//       await alert.present();
//     });
//   }

//   async deleteDiary(id: number): Promise<boolean> {
//   const alert = await this.alertCtrl.create({
//     header: '确认删除',
//     message: '确定要删除这篇日记吗？',
//     buttons: [
//       {
//         text: '取消',
//         role: 'cancel',
//         handler: () => {
//           return false;
//         }
//       },
//       {
//         text: '删除',
//         handler: () => {
//           this.confirmDelete(id);
//           return true;
//         }
//       }
//     ]
//   });
  
//   await alert.present();
//   return true;
// }

// private async confirmDelete(id: number) {
//   const diaries = await this.getDiaries();
//   const updatedDiaries = diaries.filter(d => d.id !== id);
//   localStorage.setItem(this.storageKey, JSON.stringify(updatedDiaries));
// }

async deleteDiary(id: number): Promise<boolean> {
    return new Promise<boolean>(async (resolve) => {
      const alert = await this.alertCtrl.create({
        header: '确认删除',
        message: '确定要删除这篇日记吗？',
        buttons: [
          {
            text: '取消',
            role: 'cancel',
            handler: () => resolve(false)
          },
          {
            text: '删除',
            handler: async () => {
              const diaries = await this.getDiaries();
              const updatedDiaries = diaries.filter(d => d.id !== id);
              localStorage.setItem(this.storageKey, JSON.stringify(updatedDiaries));
              resolve(true);
              // 关闭当前页面并返回tab1
            //this.modalCtrl.dismiss({ deleted: true });
            }
          }
        ]
      });
      
      await alert.present();
    });
  }
//    async onDelete(id: number) {
//     const success = await this.diaryService.deleteDiary(id);
//     if (success) {
//       // 刷新列表
//       this.loadDiaries();
//     }
//  }
}