import {Component, effect, inject, OnDestroy, OnInit} from '@angular/core';
import {ScrollService} from '@services/scroll.service';

import {HeaderComponent} from '@header/header.component';
import {SiteService, VoteData} from '@api';
import {AuthService} from '@services/auth.service';
import {interval, Subscription} from 'rxjs';
import {TimeAgoPipe} from '@pipes/TimeAgoPipe';
import {MatIconModule} from '@angular/material/icon';

@Component({
  selector: 'app-vote',
  standalone: true,
  imports: [
    HeaderComponent,
    TimeAgoPipe,
    MatIconModule,
  ],
  templateUrl: './vote.component.html',
  styleUrl: './vote.component.scss'
})
export class VoteComponent implements OnInit, OnDestroy {
  private readonly defaultVoteMessage = 'Vote!';
  private readonly clickedVoteMessage = 'Clicked!';

  private voteMessages: { [key: string]: string } = {}
  private refreshSubscription: Subscription | null = null;

  protected readonly voteSites: { [key: string]: string } = {
    'PlanetMinecraft': 'https://www.planetminecraft.com/server/alttd/vote/',
    'TopMinecraftServers': 'https://topminecraftservers.org/vote/4906',
    'Minecraft Buzz': 'https://minecraft.buzz/server/20094',
    'MinecraftServers': 'https://minecraftservers.org/vote/284208',
    'MCSL': 'https://minecraft-server-list.com/server/298238/vote/',
    'Minecraft-MP': 'https://minecraft-mp.com/server/98955/vote/',
  }

  protected scrollService: ScrollService = inject(ScrollService);
  protected siteService = inject(SiteService)
  protected authService = inject(AuthService)

  protected voteStats: VoteData | null = null

  constructor() {
    effect(() => {
      if (this.authService.isAuthenticated$()) {
        this.loadVoteStats();
      }
    });
  }

  ngOnInit(): void {
    this.refreshSubscription = interval(60000).subscribe(() => {
      this.loadVoteStats();
    });
  }

  ngOnDestroy(): void {
    this.refreshSubscription?.unsubscribe();
  }

  clickVote(id: string) {
    this.voteMessages[id] = this.clickedVoteMessage;
  }

  getVoteText(id: string) {
    return this.voteMessages[id] || this.defaultVoteMessage;
  }

  private loadVoteStats(): void {
    if (!this.authService.isAuthenticated$()) {
      return
    }
    this.siteService.getVoteStats().subscribe(voteStats => {
      this.voteStats = voteStats;
    });
  }

  protected getLastVoted(id: string): Date | null {
    if (!this.voteStats) {
      return null;
    }
    const filteredVoteInfo = this.voteStats.allVoteInfo
      .filter(voteInfo => voteInfo.siteName === id);
    if (filteredVoteInfo.length !== 1) {
      return null;
    }

    return new Date(filteredVoteInfo[0].lastVoteTimestamp);
  }

  protected readonly Object = Object;

  canVote(voteSite: string) {
    if (!this.voteStats) {
      return false;
    }
    const now: Date = new Date();
    const voteInfo = this.voteStats.allVoteInfo.find(voteInfo => voteInfo.siteName === voteSite);
    if (!voteInfo) {
      return true;
    }
    return (now.getTime() - voteInfo.lastVoteTimestamp < 86400000)
  }
}
