/*
 *
 *  Singe 2
 *  Copyright (C) 2019 Scott Duensing <scott@kangaroopunch.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */


#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <manymouse.h>

#include "stddclmr.h"
#include "common.h"
#include "util.h"
#include "videoPlayer.h"


// /home/scott/code/singe2/videotest/Stargate.m4v
// /home/scott/code/singe2/videotest/TestClip_720x480_29.97fps_2000_frames.avi


int main(int argc, char *argv[]) {

	int                         err                 =  0;
	int                         flags               =  0;
	int                         thisFrame           = -1;
	int                         lastFrame           = -1;
	int                         videoHandle         = -1;
	char                       *filename            =  argv[1];
	bool                        running             =  true;
	SDL_Window                 *window              =  NULL;
	SDL_Renderer               *renderer            =  NULL;
	SDL_Texture                *texture             =  NULL;

	// Did we get a filename to open?
	if (argc != 2) utilDie("Usage:  %s <filename>\n", argv[0]);

	// Init SDL
	err = SDL_Init(SDL_INIT_EVERYTHING);
	if (err != 0) utilDie("%s", SDL_GetError());

	// Init SDL_mixer
	flags = MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG | MIX_INIT_MID | MIX_INIT_OPUS;
	err = Mix_Init(flags);
	if (err != flags) utilDie("%s", Mix_GetError());

	// Create Resizable Window
	window = SDL_CreateWindow(filename, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_RESIZABLE);
	if (window == NULL) utilDie("%s", SDL_GetError());

	// Create an accelerated renderer.
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	if (renderer == NULL) utilDie("%s", SDL_GetError());

	// Clear screen with black
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	SDL_RenderClear(renderer);

	// Create audio mixer device
	err = Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 44100 /* freq */ * 16  /* bits */ * 2 /* channels */ * 2 /* seconds */);
	if (err != 0) utilDie("%s", Mix_GetError());

	if (videoInit()) utilDie("Unable to initialize video player.");

	videoHandle = videoLoad(filename, renderer);
	if (videoHandle < 0) utilDie("Unable to load video file:  %s", filename);

	videoPlay(videoHandle);

	while (running) {

		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			switch (event.type) {
				case SDL_KEYUP:
					switch (event.key.keysym.sym) {
						case SDLK_ESCAPE:
							running = false;
							break;
						case SDLK_SPACE:
							if (videoIsPlaying(videoHandle)) {
								videoPause(videoHandle);
							} else {
								videoPlay(videoHandle);
							}
							break;
						case SDLK_RIGHT:
							videoSeek(videoHandle, lastFrame + 1);
							break;
						case SDLK_LEFT:
							videoSeek(videoHandle, lastFrame - 1);
							break;
						case SDLK_UP:
							videoSeek(videoHandle, lastFrame + 100);
							break;
						case SDLK_DOWN:
							videoSeek(videoHandle, lastFrame - 100);
							break;
					}
					break;

				case SDL_QUIT:
					running = 0;
					break;
			}
		}

		thisFrame = videoUpdate(videoHandle, &texture);
		if ((thisFrame != lastFrame) && (thisFrame >= 0)) {
			utilSay("Presenting frame %d", thisFrame);
			lastFrame = thisFrame;
			SDL_RenderCopy(renderer, texture, NULL, NULL);
			SDL_RenderPresent(renderer);
		}
	}

	videoUnload(videoHandle);

	videoQuit();

	// Shutdown
	Mix_CloseAudio();

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	Mix_Quit();
	SDL_Quit();

	return 0;
}
