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

// Audio class method bodies
// Audio window

int audio_window_class::draw_trace(int *audio_data, int audio_data_size)
  {
  int audio_data_counter;
  int *current_audio_data_value;
  int *prev_audio_data_value;
  int this_audio_data_size;
  
  if (audio_data_size >= max_audio_trace_width)
    {
    this_audio_data_size = max_audio_trace_width;
    }
  else
    {
    this_audio_data_size = audio_data_size;
    }

  //printf("this size %d\n", this_audio_data_size);
  //Clear the window

  SDL_SetRenderDrawColor(renderer_for_audio, (Uint8)40, (Uint8)40, (Uint8)0, (Uint8)255);
  SDL_RenderClear(renderer_for_audio);

  // Render word Oscilloscope
  SDL_RenderCopy(renderer_for_audio, texture_for_audio, NULL, &rect_for_audio);
  
  // green trace
  SDL_SetRenderDrawColor(renderer_for_audio, (Uint8)0, (Uint8)(255), (Uint8)0, (Uint8)255);

  for (audio_data_counter = 1; audio_data_counter < this_audio_data_size; audio_data_counter += 1)
    {
    current_audio_data_value = audio_data + audio_data_counter;
    prev_audio_data_value = audio_data + audio_data_counter - 1;
    SDL_RenderDrawLine (renderer_for_audio, x_audio_window_trace_base + audio_data_counter -1,
      y_audio_window_trace_base + *prev_audio_data_value,
      x_audio_window_trace_base + audio_data_counter,
      y_audio_window_trace_base + *current_audio_data_value);
    }
       
  SDL_RenderPresent(renderer_for_audio);
  return 0;
  }

int audio_window_class::get_max_audio_trace_width()
  {
  return max_audio_trace_width;
  }

int audio_window_class::get_max_audio_trace_height()
  {
  return max_audio_trace_height;
  }


audio_window_class::audio_window_class (int x_main_window_size, int y_main_window_size)
  {
  x_audio_window_position = 5;
  y_audio_window_position = 5;
  x_audio_window_size = ((int)(x_main_window_size / 2)) - 20;
  y_audio_window_size = (((int)(y_main_window_size / 3)) * 2) - 10;
  x_audio_window_trace_base = x_audio_window_position;
  y_audio_window_trace_base = (int)(y_audio_window_size / 2) + y_audio_window_position;
  max_audio_trace_width = x_audio_window_size;
  max_audio_trace_height = y_audio_window_size;
  if ((window_for_audio = SDL_CreateWindow("Oscilloscope",
    x_audio_window_position, y_audio_window_position,
    x_audio_window_size, y_audio_window_size,0)) == NULL) {
      printf("error opening audio window SDL: %s\n", SDL_GetError());
      exit (0);
    }

  printf("audio window: window_size %d x pos %d max trace width %d\n",
    x_audio_window_size, x_audio_window_position, max_audio_trace_width);

  SDL_SetWindowBordered(window_for_audio, SDL_FALSE); 

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

  color_for_audio = {255,255,255};
  
  if ((surface_for_audio = TTF_RenderText_Solid(font_for_audio, 
    "OSCILLOSCOPE", color_for_audio)) == NULL) {
      printf("error opening audio surface SDL: %s\n", SDL_GetError());
      exit (0);
    }


  if ((texture_for_audio = SDL_CreateTextureFromSurface(renderer_for_audio,
    surface_for_audio)) == NULL) {
      printf("error opening audio texture SDL: %s\n", SDL_GetError());
      exit (0);
    }

  rect_for_audio.x = x_audio_window_trace_base + ((int)(max_audio_trace_width/2) -
    (int)(surface_for_audio->w / 2));
  rect_for_audio.y = 20;
  rect_for_audio.w = surface_for_audio->w;
  rect_for_audio.h = surface_for_audio->h;

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

  SDL_RenderClear(renderer_for_audio);
  SDL_RenderCopy(renderer_for_audio, texture_for_audio, NULL, &rect_for_audio);

  SDL_RenderPresent(renderer_for_audio);
  }
