import { getUserRecord, updateUserRecord } from "db/dbUser";
import { eventSub } from "index";
import { changeBalance } from "lib/changeBalance";
import { sendMessage } from "lib/commandUtils";
import { redis } from "lib/redis";
import { streamerId } from "main";
import User from "user";

eventSub.onChannelSubscription(streamerId, async (msg) => {
	await redis.set(`user:${msg.userId}:subbed`, msg.tier.slice(0, 1));
	if (msg.isGift) return;
	const user = await User.initUsername(msg.userName);
	const userRecord = await getUserRecord(user!);
	switch (msg.tier) {
		case "1000":
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE SUB ${msg.userDisplayName}! YOU GET 500 QBUCKS`,
				),
				changeBalance(user!, userRecord, 500),
			]);
			break;
		case "2000":
			userRecord.balance += 1500;
			if (userRecord.inventory.silverbullet)
				userRecord.inventory.silverbullet += 1;
			else userRecord.inventory.silverbullet = 1;
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE TIER 2 SUB ${msg.userDisplayName}! YOU GET 1500 QBUCKS AND A SILVER BULLET`,
				),
				updateUserRecord(user!, userRecord),
			]);
			break;
		case "3000":
			userRecord.balance += 3000;
			if (userRecord.inventory.silverbullet)
				userRecord.inventory.silverbullet += 2;
			else userRecord.inventory.silverbullet = 2;
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE TIER 3 SUB ${msg.userDisplayName}! YOU GET 3000 QBUCKS AND 2 SILVER BULLETS`,
				),
				updateUserRecord(user!, userRecord),
			]);
			break;
	}
});

eventSub.onChannelSubscriptionGift(streamerId, async (msg) => {
	if (msg.isAnonymous) {
		switch (msg.tier) {
			case "1000":
				await sendMessage(
					`YO THANKS ANON FOR THE SCAM SUB${msg.amount === 1 ? "" : "S"}`,
				);
				break;
			case "2000":
				await sendMessage(
					`YO THANKS ANON FOR THE ${msg.amount} TIER 2 SCAM SUB${msg.amount === 1 ? "" : "S"}`,
				);
				break;
			case "3000":
				await sendMessage(
					`YO THANKS ANON FOR THE ${msg.amount} TIER 3 SCAM SUB${msg.amount === 1 ? "" : "S"}`,
				);
				break;
		}
		return;
	}

	const user = await User.initUsername(msg.gifterName);
	const amount = msg.amount;
	const userRecord = await getUserRecord(user!);
	switch (msg.tier) {
		case "1000":
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE SCAM GIFTS ${msg.gifterDisplayName}! YOU GET ${amount * 500} QBUCKS`,
				),
				changeBalance(user!, userRecord, amount * 500),
			]);
			break;
		case "2000":
			userRecord.balance += 1500 * amount;
			if (userRecord.inventory.silverbullet)
				userRecord.inventory.silverbullet += amount;
			else userRecord.inventory.silverbullet = amount;
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE SCAM TIER 2 GIFTS ${msg.gifterDisplayName}! YOU GET ${amount * 1500} QBUCKS AND ${amount} SILVER BULLET${amount === 1 ? "" : "S"}`,
				),
				updateUserRecord(user!, userRecord),
			]);
			break;
		case "3000":
			userRecord.balance += 3000 * amount;
			if (userRecord.inventory.silverbullet)
				userRecord.inventory.silverbullet += amount * 2;
			else userRecord.inventory.silverbullet = amount * 2;
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE SCAM TIER 3 GIFTS ${msg.gifterDisplayName}! YOU GET ${amount * 3000} QBUCKS AND ${amount * 2} SILVER BULLETS`,
				),
				updateUserRecord(user!, userRecord),
			]);
			break;
	}
});

eventSub.onChannelSubscriptionEnd(streamerId, async (msg) => {
	await redis.del(`user:${msg.userId}:subbed`);
});

eventSub.onChannelSubscriptionMessage(streamerId, async (msg) => {
	await redis.set(`user:${msg.userId}:subbed`, msg.tier.slice(0, 1));
	const user = await User.initUsername(msg.userName);
	const userRecord = await getUserRecord(user!);
	switch (msg.tier) {
		case "1000":
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE RESUB ${msg.userDisplayName}! YOU GET 500 QBUCKS`,
				),
				changeBalance(user!, userRecord, 500),
			]);
			break;
		case "2000":
			userRecord.balance += 1500;
			if (userRecord.inventory.silverbullet)
				userRecord.inventory.silverbullet += 1;
			else userRecord.inventory.silverbullet = 1;
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE TIER 2 RESUB ${msg.userDisplayName}! YOU GET 1500 QBUCKS AND A SILVER BULLET`,
				),
				updateUserRecord(user!, userRecord),
			]);
			break;
		case "3000":
			userRecord.balance += 3000;
			if (userRecord.inventory.silverbullet)
				userRecord.inventory.silverbullet += 2;
			else userRecord.inventory.silverbullet = 2;
			await Promise.all([
				sendMessage(
					`YO THANKS FOR THE TIER 3 RESUB ${msg.userDisplayName}! YOU GET 3000 QBUCKS AND 2 SILVER BULLETS`,
				),
				updateUserRecord(user!, userRecord),
			]);
			break;
	}
});
