/**
 * Filesystem helpers

 * Copyright (C) 2022 Yeoldetoast

 * This file is part of YeoldeDink

 * GNU FreeDink 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 3 of the
 * License, or (at your option) any later version.

 * GNU FreeDink 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, see
 * <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "SDL.h"

#include "debug_imgui.h"
#include "game_state.h"
#include "game_engine.h"
#include "live_sprites_manager.h"
#include "input.h"
#include "log.h"
#include "dinkc.h"
#include "dinkc_console.h"
#include "freedink.h"
#include "gfx.h"

#include "imgui.h"
#include "imgui_impl_sdl.h"
#include "imgui_impl_sdlrenderer.h"

ImGuiTextBuffer Buf;
ImGuiTextFilter Filter;
ImVector <int> LineOffsets;
//For transparent overlay
static int corner = 0;

bool AutoScroll = true;
bool render_tiles = true;
char debugbuf[64];
bool textin = !!console_active;

//Toggles for smaller windows
bool ultcheat;
bool graphicsopt;
bool dswitch;
bool logwindow;
bool keeb;

void dbg_imgui(void) {
	if (debug_mode && mode) {
//Main Menu
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("File"))
{
	if (ImGui::MenuItem("Quit")) {
		dinkc_execute_one_liner("kill_game()");
	}
	ImGui::EndMenu();
}
if (ImGui::BeginMenu("View"))
{
    ImGui::MenuItem("Debug Mode Options", NULL, &dswitch);
    ImGui::MenuItem("Log Window", NULL, &logwindow);
	ImGui::MenuItem("Ultimate Cheat", NULL, &ultcheat);
	ImGui::MenuItem("Graphics Options", NULL, &graphicsopt);
    ImGui::MenuItem("Keyboard Scancodes", NULL, &keeb);

	ImGui::EndMenu();
}

ImGui::EndMainMenuBar();
//ImGui::ShowDemoWindow();

//Transparent overlay info window, stolen from the demo
ImGuiIO& io = ImGui::GetIO();
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
if (corner != -1)
    {
        const float PAD = 10.0f;
        const ImGuiViewport* viewport = ImGui::GetMainViewport();
        ImVec2 work_pos = viewport->WorkPos; // Use work area to avoid menu-bar/task-bar, if any!
        ImVec2 work_size = viewport->WorkSize;
        ImVec2 window_pos, window_pos_pivot;
        window_pos.x = (corner & 1) ? (work_pos.x + work_size.x - PAD) : (work_pos.x + PAD);
        window_pos.y = (corner & 2) ? (work_pos.y + work_size.y - PAD) : (work_pos.y + PAD);
        window_pos_pivot.x = (corner & 1) ? 1.0f : 0.0f;
        window_pos_pivot.y = (corner & 2) ? 1.0f : 0.0f;
        ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot);
        window_flags |= ImGuiWindowFlags_NoMove;
    }
	ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background
    if (ImGui::Begin("Example: Simple overlay", NULL, window_flags))
    {
        ImGui::Text("Debug mode enabled. Press Alt+D to disable, Alt+C for console");
        ImGui::Separator();
		ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
		ImGui::Text("Sprites: %d Moveman X%d X%d: Speed: %d Y%d Y%d, Screen: %d",
					last_sprite_created, spr[1].lpx[0],
					spr[1].lpy[0], spr[1].moveman, spr[1].lpx[1], spr[1].lpy[1], *pplayer_map);
        if (ImGui::IsMousePosValid())
            ImGui::Text("Raw Mouse Position: (%.1f,%.1f)", io.MousePos.x, io.MousePos.y);
        else
            ImGui::Text("Mouse Position: <invalid>");
        ImGui::Text("%s", log_getLastLog());
        if (ImGui::BeginPopupContextWindow())
        {
            if (ImGui::MenuItem("Custom",       NULL, corner == -1)) corner = -1;
            if (ImGui::MenuItem("Top-left",     NULL, corner == 0)) corner = 0;
            if (ImGui::MenuItem("Top-right",    NULL, corner == 1)) corner = 1;
            if (ImGui::MenuItem("Bottom-left",  NULL, corner == 2)) corner = 2;
            if (ImGui::MenuItem("Bottom-right", NULL, corner == 3)) corner = 3;
            if (ImGui::MenuItem("Close")) log_debug_off();
            ImGui::EndPopup();
        }
    }
    ImGui::End();


//Debug Info Window
if (dswitch) {
ImGui::Begin("Debug Mode Options");
if (ImGui::Button("Log Errors Only"))
log_debug_on(0);
ImGui::SameLine();
if (ImGui::Button("All Debug and Application Info"))
log_debug_on(1);
ImGui::SameLine();
if (ImGui::Button("Verbose"))
log_debug_on(2);
ImGui::SameLine();
if (ImGui::Button("Return to normal mode")) {
log_debug_off();
}
if (ImGui::Button("Copy last log message to clipboard")) {
ImGui::LogToClipboard();
ImGui::LogText(log_getLastLog());
ImGui::LogFinish();
}

if (ImGui::Button("Activate Console")){
	dinkc_console_show();
}
ImGui::SameLine();
if (ImGui::Button("Deactivate Console"))
dinkc_console_hide();
ImGui::End();
}

if (console_active) {
	ImGui::Begin("DinkC Console");
	ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue;
	if (ImGui::InputText("Input", debugbuf, IM_ARRAYSIZE(debugbuf), input_text_flags))
	{
		dinkc_execute_one_liner(debugbuf);
		strcpy(debugbuf, "");
	}
ImGui::End();
}

//Keyboard state
if (keeb) {
ImGui::Begin("Keyboard Scancodes");
int x;
for (x = 0; x < 256; x++)
	if (input_getcharstate(x))
	ImGui::Text("Key %i", x);
ImGui::End();
}

//Log Window
if (logwindow) {
ImGui::Begin("Log");
ImGui::Checkbox("Auto-scroll", &AutoScroll);
ImGui::Separator();
//Child window with text in it
ImGui::BeginChild("scrolling");
const char* buf = Buf.begin();
const char* buf_end = Buf.end();
ImGui::TextUnformatted(buf, buf_end);
if (AutoScroll)
ImGui::SetScrollHereY(1.0f);

ImGui::EndChild();
ImGui::End();
}

//Ultimate Cheat
if (ultcheat) {
ImGui::Begin("Ultimate Cheat");
if (ImGui::Button("Multiple Stat Boost")) {
dinkc_execute_one_liner("set_dink_speed(2)");
dinkc_execute_one_liner("sp_frame_delay(1,45)");
dinkc_execute_one_liner("&lifemax += 50");
dinkc_execute_one_liner("&magic += 50");
dinkc_execute_one_liner("&strength += 50");
dinkc_execute_one_liner("&defense += 50");
}
ImGui::End();
}

if (graphicsopt) {
//Rendering options
ImGui::Begin("Graphics");
ImGui::Checkbox("Tiles and Background Sprites", &render_tiles);
if (ImGui::Button("Fade Down"))
dinkc_execute_one_liner("fade_down()");
if (ImGui::Button("Fade up instantly"))
up_cycle();
ImGui::End();
}

}
}