#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"

// Spectrum class method bodies
// Spectrum window

int spectrum_window_class::draw_trace(int *spectrum_data, int spectrum_data_size)
  {
  int spectrum_data_counter;
  int *current_spectrum_data_value;
  int *prev_spectrum_data_value;
  int this_spectrum_data_size;
  
  if (spectrum_data_size >= max_spectrum_trace_width)
    {
    this_spectrum_data_size = max_spectrum_trace_width;
    }
  else
    {
    this_spectrum_data_size = spectrum_data_size;
    }

  //printf("this size %d\n", this_spectrum_data_size);
  //Clear the window
  SDL_SetRenderDrawColor(renderer_for_spectrum, (Uint8)40, (Uint8)40, (Uint8)0, (Uint8)255);
  SDL_RenderClear(renderer_for_spectrum);

  // Render word Spectrum
  SDL_RenderCopy(renderer_for_spectrum, texture_for_spectrum, NULL, &rect_for_spectrum);
  
  // green trace
  SDL_SetRenderDrawColor(renderer_for_spectrum, (Uint8)0, (Uint8)(255), (Uint8)255, (Uint8)255);

  for (spectrum_data_counter = 1; spectrum_data_counter < this_spectrum_data_size; spectrum_data_counter += 1)
    {
    current_spectrum_data_value = spectrum_data + spectrum_data_counter;
    prev_spectrum_data_value = spectrum_data + spectrum_data_counter - 1;
    SDL_RenderDrawLine (renderer_for_spectrum, x_spectrum_window_trace_base + spectrum_data_counter -1,
      y_spectrum_window_trace_base + *prev_spectrum_data_value,
      x_spectrum_window_trace_base + spectrum_data_counter,
      y_spectrum_window_trace_base + *current_spectrum_data_value);
    }
       
  SDL_RenderPresent(renderer_for_spectrum);
  return 0;
  }

int spectrum_window_class::get_max_spectrum_trace_width()
  {
  return max_spectrum_trace_width;
  } 

int spectrum_window_class::get_max_spectrum_trace_height()
  {
  return max_spectrum_trace_height;
  }


spectrum_window_class::spectrum_window_class (int x_main_window_size, int y_main_window_size)
  {
  x_spectrum_window_position = 30 + (int)(x_main_window_size / 2);
  y_spectrum_window_position = 5;
  x_spectrum_window_size = ((int)(x_main_window_size / 2)) - 10;
  y_spectrum_window_size = (((int)(y_main_window_size / 3)) * 2) - 10;
  x_spectrum_window_trace_base = 1;
  y_spectrum_window_trace_base = y_spectrum_window_size - 2;
  max_spectrum_trace_width = x_spectrum_window_size - 2;
  max_spectrum_trace_height = y_spectrum_window_size;
  if ((window_for_spectrum = SDL_CreateWindow("Spectrum",
    x_spectrum_window_position, y_spectrum_window_position,
    x_spectrum_window_size, y_spectrum_window_size,0)) == NULL) {
      printf("error opening spectrum window SDL: %s\n", SDL_GetError());
      exit (0);
    }

  printf("spectrum window: window_size %d x pos %d max trace width %d\n",
    x_spectrum_window_size, x_spectrum_window_position, max_spectrum_trace_width);

  SDL_SetWindowBordered(window_for_spectrum, SDL_FALSE); 

  if ((renderer_for_spectrum = SDL_CreateRenderer(window_for_spectrum, -1, 0)) == NULL) {
      printf("error opening spectrum renderer SDL: %s\n", SDL_GetError());
      exit (0);
    }
  
  if ((font_for_spectrum = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSans.ttf",
    24)) == NULL) {
      printf("error opening spectrum font SDL: %s\n", SDL_GetError());
      exit (0);
    }

  color_for_spectrum = {255,255,255};
  
  if ((surface_for_spectrum = TTF_RenderText_Solid(font_for_spectrum, 
    "SPECTRUM ANALYZER", color_for_spectrum)) == NULL) {
      printf("error opening spectrum surface SDL: %s\n", SDL_GetError());
      exit (0);
    }


  if ((texture_for_spectrum = SDL_CreateTextureFromSurface(renderer_for_spectrum,
    surface_for_spectrum)) == NULL) {
      printf("error opening spectrum texture SDL: %s\n", SDL_GetError());
      exit (0);
    }

  rect_for_spectrum.x = x_spectrum_window_trace_base + ((int)(max_spectrum_trace_width/2) -
    (int)(surface_for_spectrum->w / 2));
  rect_for_spectrum.y = 20;
  rect_for_spectrum.w = surface_for_spectrum->w;
  rect_for_spectrum.h = surface_for_spectrum->h;

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

  SDL_RenderClear(renderer_for_spectrum);
  SDL_RenderCopy(renderer_for_spectrum, texture_for_spectrum, NULL, &rect_for_spectrum);

  SDL_RenderPresent(renderer_for_spectrum);
  }
