import { Component, OnInit } from '@angular/core';

// 类型定义
interface CalendarDay {
  date: Date;
  score: number;       // 情绪指数 0-10
  peakHour?: string;   // 当日情绪峰值时间
  tags: string[];      // 情绪标签
}

interface HourlyMood {
  time: string;        // 时间点 (如 "09:00")
  score: number;       // 该时段情绪指数 0-10
}

// 仪表盘动态逻辑
interface Ripple {
  size: number;
  x: number;
  y: number;
  opacity: number;
}

@Component({
  selector: 'app-mood',
  templateUrl: 'mood.page.html',
  styleUrls: ['mood.page.scss'],
  standalone: false,
})
export class MoodPage implements OnInit {
  ripples: Ripple[] = [];

  // 添加呼吸节拍动画
  startBreathAnimation() {
    setInterval(() => {
      this.ripples.push({
        size: 0,
        x: Math.random() * 80 + 10,
        y: Math.random() * 80 + 10,
        opacity: 0.6
      });
      
      // 移除旧波纹
      if(this.ripples.length > 8) this.ripples.shift();
    }, 1500);
  }

  getRippleStyle(r: Ripple) {
    return {
      'width.px': r.size,
      'height.px': r.size,
      'left.%': r.x,
      'top.%': r.y,
      'opacity': r.opacity,
      'animation': `ripple 2s ease-out`
    };
  }

  // 当前情绪指数（示例数据）
  currentMoodScore: number = 7.8;

  // 每小时情绪数据
  hourlyMood: HourlyMood[] = [];

  offsetX = 0;
  panStartX = 0;

  handlePan(ev: any) {
    if(ev.type === 'panstart') {
      this.panStartX = this.offsetX;
    }
    if(ev.type === 'panmove') {
      this.offsetX = this.panStartX + ev.deltaX;
    }
    if(ev.type === 'panend') {
      // 惯性滑动逻辑
      const velocity = ev.velocityX * 400;
      this.offsetX += velocity;
      this.applyBounds();
    }
  }

  private applyBounds() {
    const maxOffset = -this.hourlyMood.length * 60 + 300;
    this.offsetX = Math.max(maxOffset, Math.min(0, this.offsetX));
  }

  // 蜂窝日历数据（按周分组）
  calendarWeeks: CalendarDay[][] = [];

  hexSize = 60; // 六边形边长

  getRowStyle(rowIndex: number): any {
    const offset = rowIndex % 2 === 0 ? 0 : this.hexSize * Math.cos(Math.PI/6);
    return {
      'margin-left': `${offset}px`
    };
  }
  

  // 智能分析建议
  analysisSuggestion: string = '检测到本周三下午常出现情绪波动，建议尝试呼吸练习';

  ngOnInit() {
    this.generateHourlyData();
    this.generateCalendarData();
  }

  /** 生成24小时情绪示例数据 */
  private generateHourlyData(): void {
    for (let hour = 0; hour < 24; hour++) {
      this.hourlyMood.push({
        time: `${hour.toString().padStart(2, '0')}:00`,
        score: Math.random() * 10 // 随机生成0-10分数
      });
    }
  }

  /** 生成4周日历示例数据 */
  private generateCalendarData(): void {
    const today = new Date();
    const weeks = [];
    
    // 生成4周数据
    for (let week = 0; week < 4; week++) {
      const weekDays: CalendarDay[] = [];
      
      // 每周7天
      for (let day = 0; day < 7; day++) {
        const date = new Date(today);
        date.setDate(today.getDate() - (week * 7 + day));
        
        weekDays.push({
          date,
          score: Math.random() * 10,
          peakHour: `${Math.floor(Math.random() * 24)}:00`,
          tags: this.generateRandomTags()
        });
      }
      weeks.push(weekDays);
    }
    this.calendarWeeks = weeks;
  }

  /** 生成随机情绪标签 */
  private generateRandomTags(): string[] {
    const allTags = ['平静', '愉悦', '专注', '焦虑', '疲惫', '兴奋'];
    return allTags
      .sort(() => 0.5 - Math.random())
      .slice(0, 2);
  }
  /** 获取日期情绪状态类 */
  getMoodClass(day: CalendarDay): string {
    if (day.score >= 8) return 'excellent';
    if (day.score >= 6) return 'good';
    if (day.score >= 4) return 'normal';
    return 'poor';
  }

  /** 显示日期详情 */
  showDayDetail(day: CalendarDay): void {
    console.log('选中日期详情:', day);
    // 实际开发中可跳转详情页或显示模态框
  }

  // 录音状态
  isRecording: boolean = false;

  startRecording() {
    this.isRecording = !this.isRecording;
    // 后续集成录音功能
  }
  /** 切换录音状态 */
  toggleRecording(): void {
    this.isRecording = !this.isRecording;
    if (this.isRecording) {
      this.startVoiceRecording();
    } else {
      this.stopVoiceRecording();
    }
  }

  /** 开始录音 */
  private startVoiceRecording(): void {
    console.log('开始录音...');
    // 集成实际录音逻辑
  }

  /** 结束录音 */
  private stopVoiceRecording(): void {
    console.log('结束录音...');
    // 处理录音文件
  }

  constructor() {}

}
