import { AExchangeable } from "./AExchangable.js";
import { Client } from "./Client.js";

export class Profile extends AExchangeable
{
	/**
	 * @param  {Client} client
	 */
	constructor (client, username, id, avatar)
	{
		super();

		/**
		 * @type  {Client} client
		 */
		this.client = client;
		
		/**
		 * @type {String}
		 */
		this.username = username;

		/**
		 * @type {Number}
		 */
		this.id = id;

		/**
		 * @type {String}
		 */
		this.avatar = avatar;

		/**
		 * @type {Boolean}
		 */
		this.isBlocked = false;
		this.isFriend = false;
	}

	/**
	 * 
	 * @returns {Promise<*>}
	 */
	async init()
	{
		let response;
		if (this.username !== undefined)
			response = await this.client._get(`/api/profiles/user/${this.username}`);
		else 
			response = await this.client._get(`/api/profiles/id/${this.id}`);

		if (response.status !== 200)
			return response.status;

		let response_data = await response.json();
		this.id = response_data.user_id;
		this.username = response_data.username;
		this.avatar = response_data.avatar;

	}

	/**
	 * @returns {[Object]}
	 */
	async getGameHistory()
	{
		let response = await this.client._get(`/api/games/history/${this.id}`);
		let response_data = await response.json();

		let games = [];

		response_data.forEach(game_data => {
			games.push(game_data);
		});

		return games;
	}

	/**
	 * @param {[String]} additionalFieldList
	 */
	export(additionalFieldList = [])
	{
		super.export([...["username", "avatar", "id"], ...additionalFieldList])
	}
}
