import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonLabel, IonItem, IonList, IonBackButton, IonButtons, IonIcon, IonItemDivider, IonAvatar, IonThumbnail, IonItemOptions, IonItemOption, IonItemSliding, IonInput, IonCheckbox, IonRadio, IonToggle, IonRadioGroup, IonSearchbar, IonSegment, IonSegmentButton, IonDatetime } from '@ionic/angular/standalone';
import { RouterModule } from '@angular/router';
import { SlideModule } from '../module/slide/slide.module';
import { addIcons } from 'ionicons';
import { add } from 'ionicons/icons';
// import {FmodeChatCompletion} from "fmode-ng"
import { TestChatCompletion } from './test-chat-completion';

addIcons({ add })
@Component({
  selector: 'app-me',
  templateUrl: './me.page.html',
  styleUrls: ['./me.page.scss'],
  standalone: true,
  imports: [IonContent, IonHeader, IonTitle, IonToolbar, CommonModule, FormsModule, IonButton, RouterModule, IonLabel, IonLabel, IonList, IonItem, SlideModule, IonBackButton, IonButtons, IonIcon, IonItemDivider, IonAvatar, IonThumbnail, IonItemOptions, IonItemSliding, IonItemOption, IonItemOptions, IonInput, IonCheckbox, IonRadio, IonToggle, IonRadioGroup, IonSearchbar, IonSegment, IonSegmentButton, IonDatetime]
})
export class MePage implements OnInit {
  public list: any = []
  public tab = "tab1"
  constructor() { }
  public inputValue = ""
  public gptre = ""
  public peopleInfo: any = {
    username: '',
    age: 20,
    flag: true,
    payType: '1',
    checkBoxList: [
      { val: '吃饭', ischecked: true },
      { val: '睡觉', ischecked: false },
      { val: '敲代码', ischecked: false }
    ],
    cityList: ['北京', '上海', '深圳'],
    city: '北京'
  }
  ngOnInit() {
    for (var i = 0; i < 5; i++) {
      this.list.push(`aaaaaaa${i}`);
    }
  }
  onSearchInput(event: any) {
    console.log(event.target.value);
  }
  async printInputValue() {
    console.log("开始解析");

    let token = "r:16cd8f27084ff647fcdb5308a6783c4c"
    localStorage.setItem("token",token)
    let messageList: any = [
      {
        role: "system", content: `${new Date().toLocaleString}`
      },
      {
        role: "user", content: this.inputValue
      }
    ]

    let completion=new TestChatCompletion(messageList)
    completion.createCompletionByStream()
    setInterval(()=>{
      console.log(messageList);
      this.gptre=messageList[messageList.length-1].content
    },1000)


    // let bodyJson = {
    //   "token": `Bearer ${token}`,
    //   "messages": messageList,
    //   "model": "fmode-4.5-128k",
    //   "temperature": 0.5,
    //   "presence_penalty": 0,
    //   "frequency_penalty": 0,
    //   "top_p": 1,
    //   "stream": true
    // };

    // let response = await fetch("https://test.fmode.cn/api/apig/aigc/gpt/v1/chat/completions", {
    //   "headers": {
    //     "accept": "text/event-stream",
    //   },
    //   "body": JSON.stringify(bodyJson),
    //   "method": "POST",
    //   "mode": "cors",
    //   "credentials": "omit"
    // });
    // let reader = response.body?.getReader()
    // if (!reader) {
    //   throw new Error("Failed to get the response reader.");
    // }

    // let decoder = new TextDecoder();
    // let buffer = "";

    // while (true) {
    //   let { done, value } = await reader.read();
    //   if (done) {
    //     break;
    //   }
    //   let data = decoder.decode(value);
    //   let messages = data.split("\n");
    //   console.log(messages);

    //   for (let i = 0; i < messages.length - 1; i++) {
    //     let message = messages[i];
    //     console.log(message);
    //     let dataStr: string = message.split("data: ")[1]
    //     if (dataStr && dataStr.startsWith("{")) {
    //       try {
    //         let json = JSON.parse(dataStr);
    //         let content = json.choices[0].delta.content
    //         this.gptre = this.gptre + content
    //       } catch (err) { }
    //     }


    //   }
    // }


  }
}