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

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

    async postInit()
    {
        this.profile = await client.profiles.getProfile(this.username);

		if (this.profile === null)
			return 404;
		this.user_id = this.profile.id;

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

		// Username
        let username = document.createElement("a");
		username.id = "username";
        username.appendChild(document.createTextNode(this.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();

		await this.friendButton();

		client.notice.rewrite_profile = async () => {
			let result = await this.profile.getFriend();
			await this.profile.getBlock();
			await this.friendButton();
		};
    }

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

		if (client.me.id != this.user_id) {
			let block = document.getElementById("block");
			if (block == undefined) {
				block = document.createElement("p");
				this.info.appendChild(block);
			}

			block.id = "block";
			block.onclick = async () => {
				if (!this.profile.isBlocked)
					await client.profiles.block(this.user_id);
				else
					await client.profiles.deblock(this.user_id);
				this.profile = await client.profiles.getProfile(this.username);

				this.blockButton();
			};
			if (this.profile.isBlocked)
				block.textContent = lang.get('profileUnblock', 'Unblock');
			else
				block.textContent = lang.get('profileBlock', 'Block');
		}
	}

	async friendButton() {
		if (await client.isAuthenticated() === false)
			return;

		if (client.me.id != this.user_id) {
			let yes = document.getElementById("yes") || document.createElement("p");
			let no = document.getElementById("no") || document.createElement("p");
			let friend = document.getElementById("friend") || document.createElement("p");
			
			if (client.notice.data.asker.includes(this.user_id)) {

				if (friend)
					friend.remove();

				yes.id = "yes";
				yes.textContent = lang.get('profileAcceptRequest', 'Accept Friend');
				yes.onclick = async () => {
					client.notice.accept_friend(this.user_id);
				};

				no.id = "no";
				no.textContent = lang.get('profileDenyRequest', 'Decline Friend');
				no.onclick = async () => {
					client.notice.refuse_friend(this.user_id);
				};

				this.info.appendChild(yes);
				this.info.appendChild(document.createTextNode(" "));
				this.info.appendChild(no);

			}
			else {

				if (yes && no)
					yes.remove(); no.remove();

				friend.id = "friend";
				friend.onclick = async () => {
					if (this.profile.isFriend)
						await client.notice.remove_friend(this.user_id);
					else
						await client.notice.ask_friend(this.user_id);
					await client.profiles.getProfile(this.username);
					this.friendButton();
				};
				if (this.profile.isFriend)
					friend.textContent = lang.get('profileRemoveFriend', 'Remove Friend');
				else {
					friend.textContent = lang.get('profileAddFriend', 'Ask Friend');
				}
				this.info.appendChild(friend);
			}
		}
	}

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

			</div>
        `;
    }
}
