#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <cmath>
#include <unistd.h>
#include <iostream>
#include <chrono>
#include "scope.h"

// Piano class method bodies
// Piano window

int piano_window_class::draw_trace(piano_data_struct *input_piano_data)
  {
  //printf("this size %d\n", this_piano_data_size);
  //Clear the window
  SDL_SetRenderDrawColor(renderer_for_piano, (Uint8)40, (Uint8)40, (Uint8)0, (Uint8)255);
  SDL_RenderClear(renderer_for_piano);

  // Render word Piano
  // SDL_RenderCopy(renderer_for_piano, texture_for_piano, NULL, &rect_for_piano_text);

  my_piano_data = input_piano_data;

  // point to piano_key_texture
  if (my_piano_data->keyboard_key == 0)
    {
    texture_current_piano_light = texture_key_blank;
    }
  else
    {
    texture_current_piano_light = texture_keys[my_piano_data->keyboard_key - 1];
    }
  
  // point to octive_texture
  if (my_piano_data->octive == 0)
    {
    texture_current_octive_light = texture_octive_blank;
    }
  else
    {
    texture_current_octive_light = texture_octives[my_piano_data->octive - 1];
    }
  
  // Put on screen
  SDL_RenderCopy(renderer_for_piano, texture_current_piano_light, NULL, &rect_for_keys);
  SDL_RenderCopy(renderer_for_piano, texture_current_octive_light, NULL, &rect_for_octives);

  SDL_RenderPresent(renderer_for_piano);
  return 0;
  }

int piano_window_class::get_max_piano_trace_width()
  {
  return max_piano_trace_width;
  } 

int piano_window_class::get_max_piano_trace_height()
  {
  return max_piano_trace_height;
  }


piano_window_class::piano_window_class (int x_main_window_size, int y_main_window_size)
  {
  x_piano_window_position = 15;
  y_piano_window_position = (int)((y_main_window_size / 3) * 2) + 30;
  x_piano_window_size = x_main_window_size - 40;
  y_piano_window_size = (((int)(y_main_window_size / 3)) * 1) - 10;
  x_piano_window_trace_base = 1;
  y_piano_window_trace_base = y_piano_window_size - 2;
  max_piano_trace_width = x_piano_window_size - 2;
  max_piano_trace_height = y_piano_window_size;
  if ((window_for_piano = SDL_CreateWindow("Piano",
    x_piano_window_position, y_piano_window_position,
    x_piano_window_size, y_piano_window_size,0)) == NULL) {
      printf("error opening piano window SDL: %s\n", SDL_GetError());
      exit (0);
    }

  printf("piano window: window_size %d x pos %d max trace width %d\n",
    x_piano_window_size, x_piano_window_position, max_piano_trace_width);

  SDL_SetWindowBordered(window_for_piano, SDL_FALSE); 

  if ((renderer_for_piano = SDL_CreateRenderer(window_for_piano, -1, 0)) == NULL) {
      printf("error opening piano renderer SDL: %s\n", SDL_GetError());
      exit (0);
    }
  
  // Working on the title text

  if ((font_for_piano = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSans.ttf",
    24)) == NULL) {
      printf("error opening piano font SDL: %s\n", SDL_GetError());
      exit (0);
    }

  color_for_piano = {255,255,255};
  
  if ((surface_for_piano_text = TTF_RenderText_Solid(font_for_piano, 
    "PIANO KEYBOARD", color_for_piano)) == NULL) {
      printf("error opening piano surface SDL: %s\n", SDL_GetError());
      exit (0);
    }

  if ((texture_for_piano = SDL_CreateTextureFromSurface(renderer_for_piano,
    surface_for_piano_text)) == NULL) {
      printf("error opening piano texture SDL: %s\n", SDL_GetError());
      exit (0);
    }

   rect_for_piano_text.x = x_piano_window_trace_base + 5;
   rect_for_piano_text.y = 20;
   rect_for_piano_text.w = surface_for_piano_text->w;
   rect_for_piano_text.h = surface_for_piano_text->h;

  // Load all images
  // Load blanks
  surface_key_blank = SDL_LoadBMP(
    "/home/maallyn/sound-scope/sdl2/fft/keyboard-key-blank.bmp");

  if (surface_key_blank == NULL) {
    printf("cannot load keyboard blank keymap file\n");
    printf("%s\n",SDL_GetError()); 
    printf("\n");
    exit (0);
    }

  surface_octive_blank = SDL_LoadBMP(
    "/home/maallyn/sound-scope/sdl2/fft/octive-blank.bmp");

  if (surface_octive_blank == NULL) {
    printf("cannot load octive blank keymap file\n");
    printf("%s\n",SDL_GetError()); 
    printf("\n");
    exit (0);
    }

  // Scale the rect for all keyboards (all same size)
  rect_for_keys.w = (int)(surface_key_blank->w * PIANO_SIZE_MULTIPLY);
  rect_for_keys.h = (int)(surface_key_blank->h * PIANO_SIZE_MULTIPLY);
  rect_for_keys.x = KEYBOARD_POSITION_X;
  rect_for_keys.y = KEYBOARD_POSITION_Y;

  // Scale the rect for all octives (all same size)
  rect_for_octives.w = (int)(surface_octive_blank->w * PIANO_SIZE_MULTIPLY);
  rect_for_octives.h = (int)(surface_octive_blank->h * PIANO_SIZE_MULTIPLY);
  rect_for_octives.x = OCTIVE_POSITION_X;
  rect_for_octives.y = OCTIVE_POSITION_Y;

  texture_key_blank = SDL_CreateTextureFromSurface(renderer_for_piano,
    surface_key_blank);

  if (texture_key_blank == NULL) {
    printf("cannot texture keyboard blank keymap file\n");
    printf("%s\n", SDL_GetError()); 
    printf("\n");
    exit (0);
    }

  texture_octive_blank = SDL_CreateTextureFromSurface(renderer_for_piano,
    surface_octive_blank);

  if (texture_octive_blank == NULL) {
    printf("cannot texture octive blank keymap file\n");
    printf("%s\n", SDL_GetError()); 
    printf("\n");
    exit (0);
    }

  // do rest of keys and octives
  // keys
  for (loop_counter = 0; loop_counter < NUMBER_KEY_LIGHTS; loop_counter += 1)
    {
    surface_keys[loop_counter] = SDL_LoadBMP(keyboard_lights[loop_counter]);

    if (surface_keys[loop_counter] == NULL) {
      printf("cannot load light file keymap file %s\n", keyboard_lights[loop_counter]);
      printf("%s\n",SDL_GetError()); 
      printf("\n");
      exit (0);
      }

    texture_keys[loop_counter] = SDL_CreateTextureFromSurface(renderer_for_piano,
      surface_keys[loop_counter]);

    if (texture_keys[loop_counter] == NULL) {
      printf("cannot texture keyboard light %d\n", loop_counter);
      printf("%s\n", SDL_GetError()); 
      printf("\n");
      exit (0);
      }
    }

  // octives
  for (loop_counter = 0; loop_counter < NUMBER_OCTIVE_LIGHTS; loop_counter += 1)
    {
    surface_octives[loop_counter] = SDL_LoadBMP(octive_lights[loop_counter]);

    if (surface_octives[loop_counter] == NULL) {
      printf("cannot load light file octive file %s\n", octive_lights[loop_counter]);
      printf("%s\n",SDL_GetError()); 
      printf("\n");
      exit (0);
      }

    texture_octives[loop_counter] = SDL_CreateTextureFromSurface(renderer_for_piano,
      surface_octives[loop_counter]);

    if (texture_octives[loop_counter] == NULL) {
      printf("cannot texture octive light %d\n", loop_counter);
      printf("%s\n", SDL_GetError()); 
      printf("\n");
      exit (0);
      }
    }

  

  // Put stuff on screen

  SDL_SetRenderDrawColor(renderer_for_piano, (Uint8)40, (Uint8)40, (Uint8)0, (Uint8)255);

  SDL_RenderClear(renderer_for_piano);
  // SDL_RenderCopy(renderer_for_piano, texture_for_piano, NULL, &rect_for_piano_text);
  SDL_RenderCopy(renderer_for_piano, texture_key_blank, NULL, &rect_for_keys);
  SDL_RenderCopy(renderer_for_piano, texture_octive_blank, NULL, &rect_for_octives);

  SDL_RenderPresent(renderer_for_piano);
  }
