import { Component, OnInit } from '@angular/core';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';
//import { ParseService } from '../../services/parse.service';

@Component({
  selector: 'app-thanks-cloud',
  templateUrl: './thanks-cloud.page.html',
  styleUrls: ['./thanks-cloud.page.scss'],
  standalone: false,
})
export class ThanksCloudPage implements OnInit {

  constructor(
    //private parseService: ParseService
  ) { 
    this.loadThanksType()
  }

  thanksList: CloudObject[]=[];
  async loadThanksType(){

    //修改前
    let query=new CloudQuery("ThanksType")
    this.thanksList=await query.find();
    //修改后
    //this.parseService.fetchThanksList();
  } 

  ngOnInit() {
    this.loadThanksType()
    this.thanksList=[];
  }
  async importThanks(){
    const thanksDataset = [
    {
        Tid: 1,
        date: 17,
        weekday: "周五",
        time: "07:15",
        content: "清晨被窗外的鸟鸣唤醒，发现室友已经煮好了咖啡。阳光透过窗帘的缝隙在地板上画出一道金线，突然觉得平凡的生活里藏着好多温柔。",
        weather: "晴",
        mood: "☕️",
        tags:["情感分析","行动计划"],
        reflection: "原来幸福可以这么简单，明天我要早起为室友做早餐",
        actionStep: "记录3个晨间值得感恩的细节",
        emotionalImpact: "安全感+2 | 社会联结感+1",
        emotionalJournal: "安静→温暖→感恩的情绪流动"
    },
    {​
        Tid: 2,
        date: 18,
        weekday: "周六",
        time: "14:30",
        content: "去菜市场买了把新鲜的小雏菊，回家插在旧玻璃瓶里。路过的邻居夸花好看，还分享了她的插花技巧，平凡的午后变得格外明亮。",
        weather: "多云",
        mood: "🌼",
        tags:["情感分析","生活仪式感"],
        reflection: "生活中的小美好，往往藏在与他人的善意互动里，以后要多和邻居交流",
        actionStep: "每周尝试一种新的鲜花装饰房间",
        emotionalImpact: "愉悦感 + 3 | 归属感 + 1",
        emotionalJournal: "平淡→惊喜→满足的情绪转变"
    }​,
    {​
        Tid: 3,
        date: 19,
        weekday: "周日",
        time: "19:45",
        content: "加班到很晚，疲惫地走出公司，发现地铁站口的流浪歌手在唱我喜欢的歌。驻足听了一会儿，歌声好像驱散了所有的劳累。",
        weather: "阴",
        mood: "🎵",
        tags:["情感分析","自我疗愈"],
        reflection: "音乐真的有治愈人心的力量，以后要多给自己留一些听音乐的时间",
        actionStep: "整理一份专属治愈歌单",
        emotionalImpact: "压力缓解 + 3 | 平静感 + 2",
        emotionalJournal: "疲惫→舒缓→放松的情绪过渡"
    }​,
    {​
        Tid: 4,
        date: 20,
        weekday: "周一",
        time: "12:10",
        content: "午餐时和同事拼桌，聊到小时候的趣事，大家笑得前仰后合。原来一顿简单的午饭，也能成为拉近彼此距离的桥梁。",
        weather: "晴",
        mood: "😄",
        tags:["情感分析","职场社交"],
        reflection: "工作之余的轻松交流很重要，以后要多参与同事间的互动",
        actionStep: "主动发起一次同事间的午餐聚会",
        emotionalImpact: "幸福感 + 2 | 团队融入感 + 2",
        emotionalJournal: "平淡→欢乐→温暖的情绪变化"
    }​,
    {​
        Tid: 5,
        date: 21,
        weekday: "周二",
        time: "21:30",
        content: "下雨天忘记带伞，正发愁时，一位陌生的阿姨撑着伞邀请我共行。短短一段路，却让我感受到陌生人的善意如此珍贵。",
        weather: "雨",
        mood: "🌧️→☀️",
        tags:["情感分析","陌生人善意"],
        reflection: "原来善意可以传递，下次遇到他人有困难，我也要主动帮忙",
        actionStep: "本周内为一位陌生人提供一次帮助",
        emotionalImpact: "感动 + 3 | 对世界的信任感 + 2",
        emotionalJournal: "焦虑→惊喜→温暖的情绪起伏"
    }​,
    {​
        Tid: 6,
        date: 22,
        weekday: "周三",
        time: "08:20",
        content: "晨跑时发现小区里的石榴树结了小果子，嫩绿的叶片间藏着星星点点的生机。突然觉得，只要用心观察，处处都是生命的奇迹。",
        weather: "晴",
        mood: "🌱",
        tags:["情感分析","自然观察"],
        reflection: "生活需要放慢脚步，多留意身边的美好",
        actionStep: "每天记录一个自然观察小发现",
        emotionalImpact: "愉悦感 + 2 | 对生活的热爱 + 2",
        emotionalJournal: "平常→惊喜→热爱的情绪流动"
    }
    ];
    const ThanksType=new CloudObject("ThanksType");
    const query=new CloudQuery("ThanksType");

    for(const thanks of thanksDataset){
      try{
        const query=new CloudQuery("ThanksType");
        //检查是否已存在相同内容
        query.equalTo("Tid",thanks.Tid);
        const existing=await query.first();

        if(existing){
          console.log(`清单${thanks.content}"已存在，保存跳过`);
          continue;
        }
        //创建新感恩清单
        const newThanks=new CloudObject("ThanksType");
        newThanks.set(thanks);
        //保存到数据库
        await newThanks.save();
        console.log(`清单${thanks.content}保存成功`);
      }catch(error){
        console.error(`保存清单${thanks.content}时出错`,error);
      }
    }
    console.log("所有清单数据处理完成");
  }
  
  addNewThanks(){
    console.log("添加清单")
  }
    
    
}
