import { Component, OnInit } from '@angular/core';
import * as Parse from 'parse';
import { InfiniteScrollCustomEvent } from '@ionic/angular';
import { Router } from '@angular/router';
import { AiChatService } from '../../../services/aichart.service';
import { SharedModule } from '../../shared.module';
import { MessageService } from '../../../services/message.service';
import { ionicStandaloneModules } from '../../ionic-standalone.modules';
@Component({
  selector: 'app-notice',
  templateUrl: './notice.component.html',
  styleUrls: ['./notice.component.scss'],
  standalone: true,
  imports: [SharedModule, ...ionicStandaloneModules],
})
export class NoticeComponent implements OnInit {
  active: string = 'notice';
  noticeList: Array<any> = [];
  alertButtons = [
    {
      text: '取消',
      role: 'cancel',
      handler: () => {
        console.log('Alert canceled');
      },
    },
    {
      text: '确认',
      role: 'confirm',
      handler: () => {
        console.log('Alert confirmed');
      },
    },
  ];
  friends: Array<any> = [];
  times: number = 0;
  timer: any;
  showModal: boolean = false; //是否长按弹窗
  currentObject: any; //当前选择对象
  isOpen: boolean = false;
  codeUrl: string = '';
  notices: Array<any> = [];

  constructor(
    private router: Router,
    private aiSer: AiChatService,
    public msgServe: MessageService
  ) {}

  ngOnInit() {
    this.refresh();
  }
  async refresh() {
    let uid: any = Parse.User.current()?.id;
    let resultFriends = await this.aiSer.getFriends(uid);
    this.friends = resultFriends['data'];
    console.log(this.friends);
    // let resChat = await this.aiSer.getLinkUsers(uid);
    // resChat?.data && this.noticeList.push(...resChat.data);
    this.getSetting();
    this.getSysNotice()
  }
  async getSetting() {
    let query = new Parse.Query('SiteConfig');
    query.equalTo('company', this.aiSer.company);
    query.notEqualTo('isDeleted', false);
    query.select('wxCode');
    let r = await query.first();
    this.codeUrl = r?.get('wxCode');
  }
  async getSysNotice(){
    let result = await this.aiSer.getSysNotice(Parse.User.current()?.id!)
    this.notices = result.data
    console.log(this.notices);
  }
  segmentChanged(e: any) {
    let { value } = e.detail;
    this.active = value;
  }
  /* 手指按下 */
  startPress() {
    this.showModal = false;
    if (this.times >= 500) {
      console.log(this.times);
      this.times = 0;
      this.showModal = true;
      return;
    }
    this.timer = setTimeout(() => {
      this.times += 500;
      this.startPress();
    }, 500);
  }
  /* 触摸屏幕并移动,取消长按事件 */
  onMousemove(e: any) {
    clearTimeout(this.timer);
    // this.showModal = false;
    this.times = 0;
  }
  /* 手指抬起 */
  stopPress() {
    clearTimeout(this.timer);
    this.times = 0;
  }
  onIonInfinite(ev: any) {
    console.log(ev);
    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }

  toUrl(url: string) {
    if (this.showModal) return;
    this.router.navigate([url]);
  }
  async download() {
    let canvas: any = document.createElement('canvas');
    canvas.height = '480';
    canvas.width = '480';
    let ctx = canvas.getContext('2d');
    //绘制二维码
    ctx.drawImage(await this.compileImage(this.codeUrl), 0, 0, 480, 480);
    let tempSrc = canvas.toDataURL('image/png');
    let dlLink: any = document.createElement('a');
    if ('download' in dlLink) {
      dlLink.style.visibility = 'hidden';
      dlLink.href = tempSrc;
      dlLink.download = '分享海报';
      document.body.appendChild(dlLink);
      dlLink.click();
      document.body.removeChild(dlLink);
    } else {
      location.href = tempSrc;
    }
  }

  compileImage(url: string): Promise<any> {
    return new Promise((res) => {
      let img = new Image();
      img.src = url;
      img.setAttribute('crossOrigin', 'anonymous');
      img.onload = function () {
        res(img);
      };
    });
  }
}
