import { Component } from '@angular/core';
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-tasks',
  templateUrl: './tasks.page.html',
  styleUrls: ['./tasks.page.scss'],
})
export class TasksPage {
  tasks: { startTime: string; endTime: string; content: string; editing: boolean; startHour: string; startMinute: string; endHour: string; endMinute: string; }[] = [
    { startTime: '08:00', endTime: '11:30', content: '早间任务', editing: false, startHour: '08', startMinute: '00', endHour: '11', endMinute: '30' },
    { startTime: '12:00', endTime: '17:30', content: '中间任务', editing: false, startHour: '12', startMinute: '00', endHour: '17', endMinute: '30' },
    { startTime: '18:00', endTime: '21:30', content: '晚间任务', editing: false, startHour: '18', startMinute: '00', endHour: '21', endMinute: '30' },
    // Add more tasks as needed
  ];

  constructor(private alertController: AlertController) {}

  editTask(index: number) {
    this.tasks[index].editing = true;
  }

  async saveTask(index: number) {
    const task = this.tasks[index];
    if (this.isValidTime(task.startHour, task.startMinute) && this.isValidTime(task.endHour, task.endMinute)) {
      task.startTime = `${this.padZero(task.startHour)}:${this.padZero(task.startMinute)}`;
      task.endTime = `${this.padZero(task.endHour)}:${this.padZero(task.endMinute)}`;
      task.editing = false;
      this.sortTasks();
    } else {
      const alert = await this.alertController.create({
        header: '时间错误',
        message: '小时不能超过24，分钟不能超过60。',
        buttons: ['确定']
      });
      await alert.present();
    }
  }

  addTask() {
    this.tasks.push({ startTime: '00:00', endTime: '00:00', content: '新任务', editing: true, startHour: '00', startMinute: '00', endHour: '00', endMinute: '00' });
  }

  deleteTask(index: number) {
    this.tasks.splice(index, 1);
  }

  clearTime(task: { startHour: string; startMinute: string; endHour: string; endMinute: string; }, type: 'start' | 'end') {
    if (type === 'start') {
      task.startHour = '';
      task.startMinute = '';
    } else {
      task.endHour = '';
      task.endMinute = '';
    }
  }

  formatHour(task: { startHour: string; endHour: string; }, type: 'start' | 'end') {
    if (type === 'start' && task.startHour) {
      task.startHour = this.padZero(task.startHour);
    } else if (type === 'end' && task.endHour) {
      task.endHour = this.padZero(task.endHour);
    }
  }

  formatMinute(task: { startMinute: string; endMinute: string; }, type: 'start' | 'end') {
    if (type === 'start' && task.startMinute) {
      task.startMinute = this.padZero(task.startMinute);
    } else if (type === 'end' && task.endMinute) {
      task.endMinute = this.padZero(task.endMinute);
    }
  }

  padZero(num: string): string {
    return num.length < 2 ? '0' + num : num;
  }

  isValidTime(hour: string, minute: string): boolean {
    const h = parseInt(hour, 10);
    const m = parseInt(minute, 10);
    return h >= 0 && h < 24 && m >= 0 && m < 60;
  }

  sortTasks() {
    this.tasks.sort((a, b) => {
      const aStart = parseInt(a.startHour) * 60 + parseInt(a.startMinute);
      const bStart = parseInt(b.startHour) * 60 + parseInt(b.startMinute);
      return aStart - bStart;
    });
  }
}
