<?php

namespace pizzaboxer\ProjectPolygon;

use pizzaboxer\ProjectPolygon\Database;

class Discord
{
	const WEBHOOK_POLYGON_GITPULL = "https://discord.com/api/webhooks/";
	const WEBHOOK_POLYGON_JOINLOG = "https://discord.com/api/webhooks/";
	const WEBHOOK_POLYGON_ERRORLOG = "https://discord.com/api/webhooks/";
	const WEBHOOK_KUSH = "https://discord.com/api/webhooks/";

	static function IsVerified($UserID)
	{
		$IsVerified = Database::singleton()->run("SELECT discordID FROM users WHERE id = :UserID", [":UserID" => $UserID])->fetchColumn();
		if ($IsVerified === NULL) return false;
		return true;
	}

	static function GetUserInfo($UserID)
	{
		$ch = curl_init();
		curl_setopt_array($ch, 
		[
			CURLOPT_URL => "https://discord.com/api/v8/users/$UserID", 
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_HTTPHEADER => ["Authorization: Bot"]
		]);

		$response = curl_exec($ch);
		$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if ($httpcode != 200) return false;

		$response = json_decode($response);
		if ($response == NULL) return false;

		return (object)
		[
			"username" => $response->username,
			"tag" => $response->discriminator,
			"id" => $response->id,
			"avatar" => "https://cdn.discordapp.com/avatars/{$response->id}/{$response->avatar}.png",
			"color" => $response->accent_color,
			"banner" => $response->banner,
			"banner_color" => $response->banner_color
		];
	}

	static function SendToWebhook($Payload, $Webhook, $EscapeContent = true) 
	{
	    // example payload:
	    // $payload = ["username" => "test", "content" => "test", "avatar_url" => "https://polygon.pizzaboxer.xyz/thumbs/avatar?id=1&x=100&y=100"];
	    
	    if($EscapeContent)
	    {
		    $Payload["content"] = str_ireplace(["\\", "`"], ["\\\\", "\\`"], $Payload["content"]);
			$Payload["content"] = str_ireplace(["@everyone", "@here"], ["`@everyone`", "`@here`"], $Payload["content"]);
			$Payload["content"] = preg_replace("/(<@[0-9]+>)/i", "`$1`", $Payload["content"]);
		}

	    $ch = curl_init();  

	    curl_setopt($ch, CURLOPT_URL, $Webhook);
	    curl_setopt($ch, CURLOPT_POST, true);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['payload_json' => json_encode($Payload)]));
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	    $response = curl_exec($ch);
	    curl_close($ch);

	    return $response;
	}
}