// Copyright 2018 RED Software, LLC. All Rights Reserved.
#include "MainApp.h"

#include "AppWindow.h"

#include "Folder.h"
#include "Renderer.h"


MainApp::MainApp()
{
	mpSingleton = this;

	mIsActive = false;
	mIsMinimize = false;

	mpWindow = new AppWindow;
}

HWND MainApp::GetHWND()
{
	return (mpWindow) ? mpWindow->GetHWND() : nullptr;
}

HINSTANCE MainApp::GetWindowInstance()
{
	return mpInstance;
}

MainApp* MainApp::Create()
{
	return new MainApp;
}

bool MainApp::Init(NiInstanceRef hi)
{
	//uhhh init stuff lmao

	if (!CreateMainWindow())
	{
		NiMessageBox("Failed to create window.", "Error");
		return false;
	}

}

/**
 * \brief Displays splash screen with progress bar
 * \param pcBGPath Splash background image path
 * \param pcPBPath Splash progress bar image path
 * \param ProgPercentage Progress bar percentage (0-100)
 */
void MainApp::DisplaySplash(const char* pcBGPath, const char* pcPBPath, int ProgPercentage, AppWindow *window, Renderer *renderer) const
{
    const float fDesiredAspect = 4.0f/3.0f;
	const float fAspect = static_cast<float>(window->GetWidth())/static_cast<float>(window->GetHeight());
    if (NiAbs(fAspect - fDesiredAspect) > 0.01f)
    {
            // bad aspect ratio
            NiOutputDebugString("AW: Splash aspect incorrect.\n");
    }

	const auto pkSplashData = NiNew NiScreenElementsData(false, true, 1);
	const auto pkProgressBarData = NiNew NiScreenElementsData(false, true, 1);

	auto* pkSplash = NiNew NiScreenElements(pkSplashData);
	auto* pkProgressBar = NiNew NiScreenElements(pkProgressBarData);

    pkSplash->Insert(4);
    pkProgressBar->Insert(4);
 
    pkSplash->SetRectangle(0, 0.0f, 0.0f, 1.0f, 1.0f);
    pkProgressBar->SetRectangle(0, 0.2773475f, 0.7552f, static_cast<float>(ProgPercentage)/100 * 0.5f, 0.015f); // screen
 
    pkSplash->UpdateBound();
    pkProgressBar->UpdateBound();
 
    pkSplash->SetColors(0, NiColorA::WHITE);
    pkProgressBar->SetColors(0, NiColorA::WHITE);
 
    pkSplash->SetTextures(0, 0, 0.0f, 0.0f, 1.0f, 1.0f); // texture
    pkProgressBar->SetTextures(0,0,0.0f,0.0f,static_cast<float>(ProgPercentage)/100, 1.0f);

    // textures
	auto* pkTex = NiNew NiTexturingProperty(pcBGPath);
    pkTex->SetBaseClampMode(NiTexturingProperty::CLAMP_S_CLAMP_T);

	auto* pkProgressBarTex = NiNew NiTexturingProperty(pcPBPath);
    pkProgressBarTex->SetBaseClampMode(NiTexturingProperty::CLAMP_S_CLAMP_T);
 
    pkSplash->AttachProperty(pkTex);
    pkProgressBar->AttachProperty(pkProgressBarTex);
 
    // use vertex colors
	auto* pkVertex = NiNew NiVertexColorProperty;
    pkVertex->SetSourceMode(NiVertexColorProperty::SOURCE_EMISSIVE);
    pkVertex->SetLightingMode(NiVertexColorProperty::LIGHTING_E);
 
    pkSplash->AttachProperty(pkVertex);
    pkProgressBar->AttachProperty(pkVertex);
 
    // no alpha blending
	auto* pkAlpha = NiNew NiAlphaProperty;
    pkAlpha->SetAlphaBlending(false);
 
    pkSplash->AttachProperty(pkAlpha);
    pkProgressBar->AttachProperty(pkAlpha);
 
    pkSplash->UpdateProperties();
    pkSplash->Update(0.0f);
 
    pkProgressBar->UpdateProperties();
    pkProgressBar->Update(0.0f);
 
    renderer->BeginFrame();
    renderer->BeginUsingDefaultRenderTargetGroup(NiRenderer::CLEAR_ALL);
    renderer->SetScreenSpaceCameraData();

    pkSplash->RenderImmediate(renderer);
    pkProgressBar->RenderImmediate(renderer);
 
   renderer->EndUsingRenderTargetGroup();
   renderer->EndFrame();
   renderer->DisplayFrame();
 
   delete pkSplash;
   delete pkProgressBar;
}