import AbstractView from "./abstracts/AbstractView.js";
import { client } from "../index.js"

export default class extends AbstractView {
    constructor(params) {
        super(params, params.username);
        this.username = params.username;
    }

    async postInit()
    {
        this.profile = await client.profiles.getProfile(this.username);
		if (this.profile === null)
			return 404;
		this.userId = this.profile.id;

		this.info = document.getElementById("info");

		// Username
        let username = document.createElement("a");
		username.id = "username";
        username.appendChild(document.createTextNode(this.profile.username));
		this.info.appendChild(username);

		this.info.appendChild(document.createElement("br"));

		// Avatar
        let avatar = document.createElement("img");
		avatar.id = "avatar";
        avatar.src = this.profile.avatar_url;
		this.info.appendChild(avatar);

		await this.blockButton();
    }

	async blockButton() {
		// Block option
		if (await client.isAuthentificate() === false)
			return;

		if (client.me.id != this.userId) {
			let block = document.getElementById("block") || document.createElement("a");
			block.id = "block";
			block.innerText = "";
			block.onclick = async () => {
				if (!this.profile.isBlocked)
					await client.profiles.block(this.userId);
				else
					await client.profiles.deblock(this.userId);
				this.profile = await client.profiles.getProfile(this.username);
				this.blockButton();
			};
			if (this.profile.isBlocked)
				block.appendChild(document.createTextNode("Deblock"));
			else
				block.appendChild(document.createTextNode("Block"));
			this.info.appendChild(block);
		}
	}

    async getHtml() {
        return `
            <link rel="stylesheet" href="/static/css/profile.css">
			<div id="info"> 

			</div>
        `;
    }
}
