import { Emoji, NONE_OF_THAT } from "../../server/constants"
import VoteController, { Vote, VoteMessageInfo } from "../../server/helper/vote.controller"
import { JellyfinHandler } from "../../server/jellyfin/handler"
import { ExtendedClient } from "../../server/structures/client"
import { VoteMessage } from "../../server/helper/messageIdentifiers"
import { Message, MessageReaction } from "discord.js"
test('parse votes from vote message', async () => {
	const testMovies = [
		'Movie1',
		'Movie2',
		'Movie3',
		'Movie4',
		'Movie5',
	]
	const testEventId = '1234321'
	const testEventDate = new Date('2023-01-01')
	const testGuildId = "888999888"
	const voteController: VoteController = new VoteController(<ExtendedClient>{}, <JellyfinHandler>{})
	const testMessage = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, testGuildId, "requestId")


	const expectedResult: VoteMessageInfo = {
		eventId: testEventId,
		eventDate: testEventDate,
		votes: [
			{ emote: Emoji.one, count: 1, movie: testMovies[0] },
			{ emote: Emoji.two, count: 2, movie: testMovies[1] },
			{ emote: Emoji.three, count: 3, movie: testMovies[2] },
			{ emote: Emoji.four, count: 1, movie: testMovies[3] },
			{ emote: Emoji.five, count: 1, movie: testMovies[4] },
			{ emote: NONE_OF_THAT, count: 1, movie: NONE_OF_THAT },
		]
	}

	const message: VoteMessage = <VoteMessage><unknown>{
		cleanContent: testMessage,
		guild: {
			id: testGuildId,
			scheduledEvents: {
				fetch: jest.fn().mockImplementation((input: any) => {
					if (input === testEventId)
						return {
							scheduledStartAt: testEventDate
						}
				})
			}
		},
		reactions: {
			cache: {
				get: jest.fn().mockImplementation((input: any) => {
					// Abusing duck typing
					// Message Reaction has a method `count` and the expected votes
					// have a field `count`
					// this will evaluate to the same 'result'
					return expectedResult.votes.find(e => e.emote === input)
				})
			}
		}
	}

	const result = await voteController.parseVoteInfoFromVoteMessage(message, 'requestId')
	console.log(JSON.stringify(result))
	expect(Array.isArray(result)).toBe(false)
	expect(result.eventId).toEqual(testEventId)
	expect(result.eventDate).toEqual(testEventDate)
	expect(result.votes.length).toEqual(expectedResult.votes.length)
	expect(result).toEqual(expectedResult)
})

test('parse votes from vote message', () => {
	const testMovies = [
		'Movie1',
		'Movie2',
		'Movie3',
		'Movie4',
		'Movie5',
	]
	const testEventId = '1234321'
	const testEventDate = new Date('2023-01-01')
	const testGuildId = "888999888"
	const voteController: VoteController = new VoteController(<ExtendedClient>{}, <JellyfinHandler>{})
	const testMessage = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, testGuildId, "requestId")

	const result = voteController.parseGuildIdAndEventIdFromWholeMessage(testMessage)
	expect(result).toEqual({ guildId: testGuildId, eventId: testEventId })
})


test.skip('handles complete none_of_that vote', () => {

	const mockJellyfinHandler: JellyfinHandler = <JellyfinHandler><unknown>{
		getRandomMovieNames: jest.fn().mockReturnValue(["movie1"])
	}

	const testMovies = [
		'Movie1',
		'Movie2',
		'Movie3',
		'Movie4',
		'Movie5',
	]
	const testEventId = '1234321'
	const testEventDate = new Date('2023-01-01')
	const testGuildId = "888999888"
	const mockClient: ExtendedClient = <ExtendedClient><unknown>{
		user: {
			id: 'mockId'
		}
	}
	const voteController = new VoteController(mockClient, mockJellyfinHandler)
	const mockMessageContent = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, testGuildId, "requestId")
	const reactedUponMessage: VoteMessage = <VoteMessage><unknown>{
		cleanContent: mockMessageContent,
		guild: {
			id: 'id',
			roles: {
				resolve: jest.fn().mockReturnValue({
					members: [{}, {}, {}, {}, {}]//content does not matter
				})
			},
			scheduledEvents: {
				fetch: jest.fn().mockReturnValue([
					{
						name: 'voting offen'
					}
				])
			}
		},
		unpin: jest.fn().mockImplementation(() => {

		}),
		delete: jest.fn().mockImplementation(() => {

		}),
		reactions: {
			resolve: jest.fn().mockImplementation((input: any) => {
				console.log(JSON.stringify(input))
			}),
			cache: {
				get: jest.fn().mockReturnValue({
					users: {
						cache: [
							{
								id: "mockId"//to filter out
							},
							{
								id: "userId1"
							},
							{
								id: "userId2"
							},
							{
								id: "userId3"
							}
						]
					}
				})
			}
		}
	}
	const messageReaction: MessageReaction = <MessageReaction><unknown>{
		message: reactedUponMessage
	}

	mockClient.getAnnouncementChannelForGuild = jest.fn().mockReturnValue({
		messages: {
			fetch: jest.fn().mockReturnValue([
				reactedUponMessage
			])
		}
	})

	const res = voteController.handleNoneOfThatVote(messageReaction, reactedUponMessage, 'requestId', 'guildId')


})
