import { Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { API_URL } from "../../app.constants";
import { AppState } from "../../types/app-state";
import { Participant } from "../../types/participant";
import { Question } from "../../types/question";
import { CardItem } from "../../types/card-item";
import { GameState } from "./gameState";
import { PenaltyDto } from "../../types/penalty.dto";
import { PrizeDto } from "../../types/prize.dto";
import {QuestionresultsDto} from "../../types/questionresults.dto";
import {map} from "rxjs/operators";
import {VersusItem} from "../../types/versus-item";

export class FeatureFlagStateDto {
  name: string;
  state: boolean;
}

export interface StateInformationDto<T extends { action: string}> {
  key: string;
  value: { key: string; value: T };
}

export interface StateInformationVersusDto {
  action: any;
  player1: number;
  player2: number;
}

export interface ConfigRecordDto {
  key: string;
  value: string;
}

export interface EndgameResultsDto {
  maxInvalidAnswers: {
    id: number;
    count: number;
    name: string;
  },
  maxRewards: {
    id: number;
    count: number;
    name: string;
  },
  maxPenalties: {
    id: number;
    count: number;
    name: string;
  }
}

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor(private httpClient: HttpClient) { }

  public getAppState(state: string): Observable<AppState> {
    return this.httpClient.get<AppState>(`${API_URL}/state/${state}`);
  }

  public getParticipants(): Observable<Participant[]> {
    return this.httpClient.get<Participant[]>(`${API_URL}/guests`);
  }

  public getParticipant(id: number): Observable<Participant> {
    return this.httpClient.get<Participant>(`${API_URL}/guests/${id}`);
  }

  public getQuestion(): Observable<Question> {
    return this.httpClient.get<Question>(`${API_URL}/quiz`);
  }

  public setAppState(state: string, value: string) {
        return this.httpClient.post<AppState>(`${API_URL}/state`, {
          state,
          value
        });
    }

    getCards(telegramId: number): Observable<CardItem[]> {
        return this.httpClient.get<CardItem[]>(`${API_URL}/cards/${telegramId}`);
    }

  continueGame() {
    console.log(`continue game`);
    return this.httpClient.post(`${API_URL}/quiz/proceed`, {});
  }

  markQueueAsCompleted(_id: string) {
    return this.httpClient.post(`${API_URL}/game/${_id}/complete`, {});
  }

    pauseGame() {
        return this.httpClient.post(`${API_URL}/game/pause`, {});
    }

    resumeGame() {
      return this.httpClient.post(`${API_URL}/game/resume`, {});
    }

  getGameState() {
    return this.httpClient.get<GameState>(`${API_URL}/game/state`);
  }

  getPenalty() {
    console.log(`get penalty`);
    return this.httpClient.get<PenaltyDto>(`${API_URL}/penalty`);
  }

  playExtraCards() {
    console.log(`play extra cards`);
    return this.httpClient.get(`${API_URL}/game/playextracards`);
  }

    getAdditionalQuestion(target: number) {
        return this.httpClient.post<Question>(`${API_URL}/quiz/extraquestion`, {
          telegramId: target,
        });
    }

  getImageUrl(id: number) {
    const timestamp = new Date().getTime();
    return `${API_URL}/guests/photo/${id}?$t=${timestamp}}`;
  }

  getPrize(): Observable<PrizeDto> {
    return this.httpClient.get<PrizeDto>(`${API_URL}/gifts`);
  }

  getQuestionResults() {
    return this.httpClient.get<QuestionresultsDto[]>(`${API_URL}/quiz/question-results`).pipe(map((data) =>
      data.map((item) => {
        return {
          ...item,
          time: new Date(item.time)
        }
      })
    ));
  }

  getFeatureFlagState(feature: string) {
    return this.httpClient.get<FeatureFlagStateDto>(`${API_URL}/featureflag/${feature}`);
  }

  setFeatureFlagState(feature: string, state: boolean) {
    return this.httpClient.post<FeatureFlagStateDto>(`${API_URL}/featureflag`, { name: feature, state: state });
  }

  getStateDetails()  {
    return this.httpClient.get<ConfigRecordDto>(`${API_URL}/game/state-details`);
  }

  getVersus() {
    return this.httpClient.get<VersusItem>(`${API_URL}/versus`);
  }

  completeVersus(winner: number, loser: number) {
    return this.httpClient.post(`${API_URL}/versus/complete`, {
      winner: winner,
      loser: loser
    });
  }

  getEndgameResults() {
    return this.httpClient.get<EndgameResultsDto>(`${API_URL}/quiz/endgame-results`)
  }
}
