#include "App.hh"

namespace TTT
{

App::App(int width, int height) : 
    m_WindowHeight(height), m_WindowWidth(width), m_Close(0), m_GridHeight(5), m_GridWidth(5)
{
    // returns zero on success else non-zero
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        SDL_LogCritical(0,"error initializing SDL: %s\n", SDL_GetError());
    }
    m_Window = SDL_CreateWindow("Tic-Tac-Toe", // creates a window
                                       SDL_WINDOWPOS_CENTERED,
                                       SDL_WINDOWPOS_CENTERED,
                                       m_WindowHeight, m_WindowHeight, 0);

    // Initialize grid. Look at this transposed.
    std::pair<SDL_Rect, TEXTURE> pair = std::pair<SDL_Rect, TEXTURE>();
    pair.second = WALL;
    m_Grid = {
        { pair, pair, pair, pair, pair },
        { pair, pair, pair, pair, pair },
        { pair, pair, pair, pair, pair },
        { pair, pair, pair, pair, pair },
        { pair, pair, pair, pair, pair }
    };

    // m_Grid[0][0].second = NONE;

    m_Board = std::make_shared<Board>();

    // triggers the program that controls
    // your graphics hardware and sets flags
    Uint32 render_flags = SDL_RENDERER_ACCELERATED;
 
    // creates a renderer to render our images
    m_Renderer = SDL_CreateRenderer(m_Window, -1, render_flags);
 
    // please provide a path for your image
    SDL_Surface* wall_surface = IMG_Load("./res/TTT_WALL.png");
    SDL_Surface* symbol_x_surface = IMG_Load("./res/SYMBOL_X.png");
    SDL_Surface* symbol_o_surface = IMG_Load("./res/SYMBOL_O.png");
 
    // loads image to our graphics hardware memory.
    m_Textures[TTT::WALL] = SDL_CreateTextureFromSurface(m_Renderer, wall_surface);
    m_Textures[TTT::SYMBOL_X] = SDL_CreateTextureFromSurface(m_Renderer, symbol_x_surface);
    m_Textures[TTT::SYMBOL_O] = SDL_CreateTextureFromSurface(m_Renderer, symbol_o_surface);
    
    // clears surfaces from main-memory.
    SDL_FreeSurface(wall_surface);
    SDL_FreeSurface(symbol_x_surface);
    SDL_FreeSurface(symbol_o_surface);
 
    DrawGrid();

    // speed of box
    int speed = 300;
 
    // animation loop
    while (!m_Close) {
        SDL_Event event;
 
        // Events management
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
 
            case SDL_QUIT:
                // handling of close button
                m_Close = 1;
                break;
            case SDL_MOUSEBUTTONDOWN:
                switch (event.button.button)
                {
                case SDL_BUTTON(SDL_BUTTON_LEFT):
                    SDL_LogInfo(0, "LEFT CLICK (%d, %d)\n", event.motion.x, event.motion.y);
                    break;
                default:
                    break;
                }
                break;
            case SDL_KEYDOWN:
                // keyboard API for key pressed
                switch (event.key.keysym.scancode) {
                case SDL_SCANCODE_ESCAPE:
                    m_Close = 1;
                    break;
                case SDL_SCANCODE_W:
                case SDL_SCANCODE_UP:
                    break;
                case SDL_SCANCODE_A:
                case SDL_SCANCODE_LEFT:
                    break;
                case SDL_SCANCODE_S:
                case SDL_SCANCODE_DOWN:
                    break;
                case SDL_SCANCODE_D:
                case SDL_SCANCODE_RIGHT:
                    break;
                default:
                    break;
                }
            }
        }
        GridRenderCopy();
 
        // triggers the double buffers
        // for multiple rendering
        SDL_RenderPresent(m_Renderer);
 
        // calculates to 60 fps
        SDL_Delay(1000 / 60);
    }
 
    // destroy texture
    SDL_DestroyTexture(m_Textures[TTT::SYMBOL_O]);
}

void App::DrawToGridCell(TEXTURE textureId, int x, int y)
{

}

void App::DrawGrid()
{
    int CELL_WIDTH = 64;
    int CELL_HEIGHT = 64;
    

    for (int i = 0; i <= m_GridWidth - 1; ++i)
    {
        for (int j = 0; j <= m_GridHeight - 1; ++j)
        {
            // connects our texture with dest to control position
            SDL_QueryTexture(
                m_Textures[m_Grid[i][j].second], NULL, NULL, 
                &m_Grid[i][j].first.w, &m_Grid[i][j].first.h);

            // m_Grid[i][j].first.x = (m_WindowWidth - m_Grid[i][j].first.w) / 2;
            // m_Grid[i][j].first.y = (m_WindowHeight - m_Grid[i][j].first.h) / 2;
            m_Grid[i][j].first.w = CELL_WIDTH/8;
            m_Grid[i][j].first.h = CELL_HEIGHT/8;
            m_Grid[i][j].first.x = m_Grid[i][j].first.w * i;
            m_Grid[i][j].first.y = m_Grid[i][j].first.h * j;
        }   
    }
    
}

void App::UpdateGridHover(int x, int y)
{
}

void App::GridRenderCopy()
{
    for (int i = 0; i <= m_GridWidth - 1; ++i)
    {
        for (int j = 0; j <= m_GridHeight - 1; ++j)
        {
            SDL_RenderCopy(m_Renderer, m_Textures[m_Grid[i][j].second], NULL, &m_Grid[i][j].first);
        }   
    }    
}

App::~App()
{
    
    // destroy renderer
    SDL_DestroyRenderer(m_Renderer);
    // destroy window
    SDL_DestroyWindow(m_Window);
    // close SDL
    SDL_Quit();
}


} // namespace TTT