#include "loggers/FileLogger.h"

#include <DirectXMath.h>
#include <DirectXPackedVector.h>
#include <iostream>
#include <vector>

#include <windows.h>
#include <wrl.h>

#include <dxgi.h>
#include <dxgi1_6.h>
#include <d3d12.h>
#include <d3d12sdklayers.h>


void GetHardwareAdapter(IDXGIFactory7* pFactory, IDXGIAdapter1** ppAdapter)
{
	*ppAdapter = nullptr;
	for (UINT adapterIndex = 0; ; ++adapterIndex)
	{
		IDXGIAdapter1* pAdapter = nullptr;
		if (DXGI_ERROR_NOT_FOUND == pFactory->EnumAdapters1(adapterIndex, &pAdapter))
		{
			// No more adapters to enumerate.
			break;
		}

		// Check to see if the adapter supports Direct3D 12, but don't create the
		// actual device yet.
		if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
		{
			*ppAdapter = pAdapter;
			return;
		}
		pAdapter->Release();
	}
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
	FileLogger::GetInstance().Open();
	MLOG_INFO("Test");

	if (!DirectX::XMVerifyCPUSupport())
	{
		MLOG_ERROR("Directx math not supported");
		return 0;
	}

	auto n = DirectX::XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
	//auto u = DirectX::XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
	//auto v = DirectX::XMVectorSet(-2.0f, 1.0f, -3.0f, 0.0f);
	//auto w = DirectX::XMVectorSet(0.707f, 0.707f, 0.0f, 0.0f);

	auto p = DirectX::XMVectorZero();
	auto q = DirectX::XMVectorSplatOne();
	auto u = DirectX::XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
	auto v = DirectX::XMVectorReplicate(-2.0f);
	auto w = DirectX::XMVectorSplatZ(u);

	DirectX::XMMATRIX A(1.0f, 0.0f, 0.0f, 0.0f,
		0.0f, 2.0f, 0.0f, 0.0f,
		0.0f, 0.0f, 4.0f, 0.0f,
		1.0f, 2.0f, 3.0f, 1.0f);
	DirectX::XMMATRIX B = DirectX::XMMatrixIdentity();

	IDXGIAdapter* adapter = nullptr;
	std::vector<IDXGIAdapter*> adapterList;

	Microsoft::WRL::ComPtr<ID3D12CommandQueue> commandQueue;
	D3D12_COMMAND_QUEUE_DESC queueDesc = {};
	queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;

#if defined(DEBUG) || defined(_DEBUG)
	// Enable the D3D12 debug layer.
	{
		ID3D12Debug* debugController;
		D3D12GetDebugInterface(IID_PPV_ARGS(&debugController));
		debugController->EnableDebugLayer();
	}
#endif

	IDXGIFactory7* pFactory{ nullptr };
	HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory7), (void**)(&pFactory));
	if (hr == S_OK)
	{
		MLOG_INFO("FACTORY IS OK");
	}

	IDXGIAdapter1* pAdapter{ nullptr };
	GetHardwareAdapter(pFactory, &pAdapter);

	ID3D12Device* pDevice{ nullptr };
	if (pAdapter)
	{
		MLOG_INFO("Found adapter");
		if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice))))
		{
			MLOG_INFO("Got device");
		}

	}
	else
	{
		MLOG_INFO("No adapter found");
		return 0;
	}

	return 0;
}