#include "TextureModel.h"

#include "DXBuffer.h"
#include "Globals.h"
#include "GraphicsApi.h"
#include "Model3DList.h"
#include "Texture.h"
#include "TextureList.h"

using namespace Framework;

// Contents of the TextureModel class

// Constructor
TextureModel::TextureModel(GraphicsApi* zApi)
    : TextureModel(zApi, Standart3DTypes::texturModel)
{}

Framework::TextureModel::TextureModel(GraphicsApi* zApi, const char* uniqueName)
{
    if (zApi->hasModel(uniqueName))
        model = zApi->getModel(uniqueName);
    else
    {
        model = zApi->createModel(uniqueName);
        Vertex3D* vertecies = new Vertex3D[4];
        for (int i = 0; i < 4; i++)
            vertecies[i].knochenId = 0;
        vertecies[0].pos = Vec3<float>(-50, 50, 0.f);
        vertecies[0].tPos = Vec2<float>(0.f, 0.f);
        vertecies[1].pos = Vec3<float>(50, 50, 0.f);
        vertecies[1].tPos = Vec2<float>(1.f, 0.f);
        vertecies[2].pos = Vec3<float>(-50, -50, 0.f);
        vertecies[2].tPos = Vec2<float>(0.f, 1.f);
        vertecies[3].pos = Vec3<float>(50, -50, 0.f);
        vertecies[3].tPos = Vec2<float>(1.f, 1.f);

        model->setVertecies(vertecies, 4);
        Polygon3D* p = new Polygon3D();
        p->indexAnz = 6;
        p->indexList = new int[p->indexAnz];
        p->indexList[0] = 0;
        p->indexList[1] = 3;
        p->indexList[2] = 2;
        p->indexList[3] = 0;
        p->indexList[4] = 1;
        p->indexList[5] = 3;
        model->addPolygon(p);

        model->calculateNormals();
    }
    texture = new Model3DTexture();
}

// Sets the texture to be displayed
//  t: The texture
void TextureModel::setTexture(Texture* t)
{
    if (!t) return;
    this->texture->setPolygonTexture(0, t);
    rend = 1;
}

// Sets the size in which the texture is displayed
//  gr: A vector containing width and height for x and y
void TextureModel::setSize(Vec2<float> gr)
{
    gr /= 2;
    Vertex3D* vertecies = new Vertex3D[4];
    for (int i = 0; i < 4; i++)
        vertecies[i].knochenId = 0;
    vertecies[0].pos = Vec3<float>(-gr.x, gr.y, 0.f);
    vertecies[0].tPos = Vec2<float>(0.f, 0.f);
    vertecies[1].pos = Vec3<float>(gr.x, gr.y, 0.f);
    vertecies[1].tPos = Vec2<float>(1.f, 0.f);
    vertecies[2].pos = Vec3<float>(-gr.x, -gr.y, 0.f);
    vertecies[2].tPos = Vec2<float>(0.f, 1.f);
    vertecies[3].pos = Vec3<float>(gr.x, -gr.y, 0.f);
    vertecies[3].tPos = Vec2<float>(1.f, 1.f);
    model->setVertecies(vertecies, 4);
}

// Sets the size in which the texture is displayed
//  b: The width in which the texture is displayed
//  h: The height in which the texture is displayed
void TextureModel::setSize(float b, float h)
{
    b /= 2;
    h /= 2;
    Vertex3D* vertecies = new Vertex3D[4];
    for (int i = 0; i < 4; i++)
        vertecies[i].knochenId = 0;
    vertecies[0].pos = Vec3<float>(-b, h, 0.f);
    vertecies[0].tPos = Vec2<float>(0.f, 0.f);
    vertecies[1].pos = Vec3<float>(b, h, 0.f);
    vertecies[1].tPos = Vec2<float>(1.f, 0.f);
    vertecies[2].pos = Vec3<float>(-b, -h, 0.f);
    vertecies[2].tPos = Vec2<float>(0.f, 1.f);
    vertecies[3].pos = Vec3<float>(b, -h, 0.f);
    vertecies[3].tPos = Vec2<float>(1.f, 1.f);
    model->setVertecies(vertecies, 4);
}