#pragma once
#include "Rectangle.h"
#include "Bindable.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
#include "VertexShader.h"
#include "PixelShader.h"
#include "ConstantBuffer.h"
#include "InputLayout.h"
#include "Topology.h"
#include "TransformCBuf.h"
#include "Graphics.h"
#include <DirectXMath.h>
#include "Texture.h"
#include "Structs.h"

Rectangle::Rectangle(Graphics& gfx, StructVertexWithColor vertices[], Logger& logger)
{
	/************************************
	* 2.3 DefineVertexBuffer
	************************************/
	std::vector<StructVertexWithColor> vb;
	for (int i = 0; i < 4; i++)
	{
		vb.push_back(vertices[i]);
	}

	AddBind(std::make_unique<VertexBuffer>(gfx, vb, logger));

	//AddBind(std::make_unique<PixelConstantBuffer<StructPixelConstBuf>>(gfx, cb2));

	/************************************
	* 2.5 DefineTransConstBuffer
	************************************/
	AddBind(std::make_unique<TransformCbuf>(gfx, *this));

	if (!IsStaticInitialized())
	{
		/************************************
		* 2.7 DefineVertexShader
		************************************/
		auto pVertexShader = std::make_unique<VertexShader>(gfx, L"ColorVS.cso");
		auto pvsbc = pVertexShader->GetBytecode();
		//AddBind(std::move(pVertexShader));
		AddStaticBind(std::move(pVertexShader));

		/************************************
		* 2.8 DefinePixelShader
		************************************/
		AddStaticBind(std::make_unique<PixelShader>(gfx, L"ColorPS.cso"));

		/************************************
		* 2.4 DefineIndexBuffer
		************************************/
		const std::vector<unsigned short> indices =
		{
			0,1,2, 2,1,3,
		};

		AddStaticIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices, logger));

		/************************************
		* 2.9 DefineInputLayout
		************************************/
		const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
		{
			{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
			{ "Color",0,DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 },
		};

		AddStaticBind(std::make_unique<InputLayout>(gfx, ied, pvsbc));

		/************************************
		* 2.10 DefinePrimitiveTopology
		************************************/
		AddStaticBind(std::make_unique<Topology>(gfx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST));
	}
	else
	{
		SetIndexFromStatic();
	}
}

void Rectangle::Update(float dt) noexcept
{
	/*roll += droll * dt;
	pitch += dpitch * dt;
	yaw += dyaw * dt;
	theta += dtheta * dt;
	phi += dphi * dt;
	chi += dchi * dt;*/
	//dx = dt;
	//dy = dt;
	//dz = dt * 10.0f;
}

//void Box::UpdateCamera(float sin, float cos) noexcept
//{
//	using namespace DirectX;
//	upDirection = XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);
//	focusPosition = XMVectorZero();
//	eyePosition = XMVectorSet(20.0f * sin, 10.0f, -20.0f * cos, 1.0f);
//};

DirectX::XMMATRIX Rectangle::GetTransformXM(Graphics& gfx) const noexcept
{
	DirectX::XMMATRIX lookAtMatrix = gfx.getLookAtMatrix();

	return
		DirectX::XMMatrixTranslation(dx, dy, dz)
		* lookAtMatrix;
}