/**
 * Furnace Tracker - multi-system chiptune tracker
 * Copyright (C) 2021-2023 tildearrow and contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "gui.h"
#include "imgui_internal.h"
#include <imgui.h>
#include "../ta-log.h"
#include "../engine/filter.h"

void FurnaceGUI::readOsc() {
  int writePos=e->oscWritePos;
  int readPos=e->oscReadPos;
  int avail=0;
  int total=0;
  if (firstFrame) {
    readPos=writePos;
  }
  if (writePos>=readPos) {
    avail=writePos-readPos;
  } else {
    avail=writePos-readPos+32768;
  }
  if (oscTotal==0) {
    oscTotal=ImGui::GetIO().DeltaTime*e->getAudioDescGot().rate;
  } else {
    oscTotal=(oscTotal+(int)round(ImGui::GetIO().DeltaTime*e->getAudioDescGot().rate))>>1;
  }
  int bias=avail-oscTotal-e->getAudioDescGot().bufsize;
  if (bias<0) bias=0;
  total=oscTotal+(bias>>6);
  if (total>avail) total=avail;
  //printf("total: %d. avail: %d bias: %d\n",total,avail,bias);

  int winSize=e->getAudioDescGot().rate*(oscWindowSize/1000.0);
  int oscReadPos=(writePos-winSize)&0x7fff;

  for (int ch=0; ch<e->getAudioDescGot().outChans; ch++) {
    if (oscValues[ch]==NULL) {
      oscValues[ch]=new float[1024];
    }
    memset(oscValues[ch],0,1024*sizeof(float));
    float* sincITable=DivFilterTables::getSincIntegralTable();

    float posFrac=0.0;
    int posInt=oscReadPos;
    float factor=(float)oscWidth/(float)winSize;
    for (int i=0; i<oscWidth; i++) {
      oscValues[ch][i]+=e->oscBuf[ch][posInt&0x7fff];

      posFrac+=1.0;
      while (posFrac>=1.0) {
        unsigned int n=((unsigned int)(posFrac*8192.0))&8191;
        posFrac-=factor;
        posInt++;

        float* t1=&sincITable[(8191-n)<<3];
        float* t2=&sincITable[n<<3];
        float delta=e->oscBuf[ch][posInt&0x7fff]-e->oscBuf[ch][(posInt-1)&0x7fff];

        for (int j=0; j<8; j++) {
          if (i-j>0) {
            oscValues[ch][i-j]+=t1[j]*-delta;
          }
          if (i+j+1<oscWidth) {
            oscValues[ch][i+j+1]+=t2[j]*delta;
          }
        }
      }
    }

    for (int i=0; i<oscWidth; i++) {
      if (oscValues[ch][i]>0.001f || oscValues[ch][i]<-0.001f) {
        WAKE_UP;
      }
    }
  }

  /*for (int i=0; i<oscWidth; i++) {
    oscValues[i]=(i&1)?0.3:0;
  }*/

  float peakDecay=0.05f*60.0f*ImGui::GetIO().DeltaTime;
  for (int i=0; i<e->getAudioDescGot().outChans; i++) {
    peak[i]*=1.0-peakDecay;
    if (peak[i]<0.0001) {
      peak[i]=0.0;
    } else {
      WAKE_UP;
    }
    float newPeak=peak[i];
    for (int j=0; j<total; j++) {
      int pos=(readPos+j)&0x7fff;
      if (fabs(e->oscBuf[i][pos])>newPeak) {
        newPeak=fabs(e->oscBuf[i][pos]);
      }
    }
    peak[i]+=(newPeak-peak[i])*0.9;
  }

  readPos=(readPos+total)&0x7fff;
  e->oscReadPos=readPos;
}

void FurnaceGUI::drawOsc() {
  if (nextWindow==GUI_WINDOW_OSCILLOSCOPE) {
    oscOpen=true;
    ImGui::SetNextWindowFocus();
    nextWindow=GUI_WINDOW_NOTHING;
  }
  if (!oscOpen) return;
  ImGui::SetNextWindowSizeConstraints(ImVec2(64.0f*dpiScale,32.0f*dpiScale),ImVec2(canvasW,canvasH));
  if (settings.oscTakesEntireWindow) {
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding,ImVec2(0,0));
    ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,ImVec2(0,0));
    ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing,ImVec2(0,0));
  }
  if (ImGui::Begin("Oscilloscope",&oscOpen,globalWinFlags)) {
    if (oscZoomSlider) {
      if (ImGui::VSliderFloat("##OscZoom",ImVec2(20.0f*dpiScale,ImGui::GetContentRegionAvail().y),&oscZoom,0.5,2.0)) {
        if (oscZoom<0.5) oscZoom=0.5;
        if (oscZoom>2.0) oscZoom=2.0;
      } rightClickable
      if (ImGui::IsItemHovered()) {
        ImGui::SetTooltip("zoom: %.2fx (%.1fdB)",oscZoom,20.0*log10(oscZoom*2.0));
      }
      if (ImGui::IsItemClicked(ImGuiMouseButton_Middle)) {
        oscZoom=0.5;
      }
      ImGui::SameLine();
      if (ImGui::VSliderFloat("##OscWinSize",ImVec2(20.0f*dpiScale,ImGui::GetContentRegionAvail().y),&oscWindowSize,5.0,100.0)) {
        if (oscWindowSize<5.0) oscWindowSize=5.0;
        if (oscWindowSize>100.0) oscWindowSize=100.0;
      } rightClickable
      if (ImGui::IsItemHovered()) {
        ImGui::SetTooltip("window size: %.1fms",oscWindowSize);
      }
      if (ImGui::IsItemClicked(ImGuiMouseButton_Middle)) {
        oscWindowSize=20.0;
      }
      ImGui::SameLine();
    }

    ImDrawList* dl=ImGui::GetWindowDrawList();
    ImGuiWindow* window=ImGui::GetCurrentWindow();

    ImVec2 waveform[1024];
    ImVec2 size=ImGui::GetContentRegionAvail();

    ImVec2 minArea=window->DC.CursorPos;
    ImVec2 maxArea=ImVec2(
      minArea.x+size.x,
      minArea.y+size.y
    );
    ImRect rect=ImRect(minArea,maxArea);
    ImRect inRect=rect;
    inRect.Min.x+=dpiScale;
    inRect.Min.y+=dpiScale;
    inRect.Max.x-=dpiScale;
    inRect.Max.y-=dpiScale;
    ImGuiStyle& style=ImGui::GetStyle();
    ImU32 color=ImGui::GetColorU32(isClipping?uiColors[GUI_COLOR_OSC_WAVE_PEAK]:uiColors[GUI_COLOR_OSC_WAVE]);
    ImU32 borderColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BORDER]);
    ImU32 refColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_REF]);
    ImU32 guideColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_GUIDE]);
    ImGui::ItemSize(size,style.FramePadding.y);
    if (ImGui::ItemAdd(rect,ImGui::GetID("wsDisplay"))) {
      dl->AddRectFilledMultiColor(
        inRect.Min,
        inRect.Max,
        ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG1]),
        ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG2]),
        ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG4]),
        ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG3]),
        settings.oscRoundedCorners?(8.0f*dpiScale):0.0f
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.0f,0.5f)),
        ImLerp(rect.Min,rect.Max,ImVec2(1.0f,0.5f)),
        refColor,
        dpiScale
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.48f,0.125f)),
        ImLerp(rect.Min,rect.Max,ImVec2(0.52f,0.125f)),
        guideColor,
        dpiScale
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.47f,0.25f)),
        ImLerp(rect.Min,rect.Max,ImVec2(0.53f,0.25f)),
        guideColor,
        dpiScale
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.45f,0.375f)),
        ImLerp(rect.Min,rect.Max,ImVec2(0.55f,0.375f)),
        guideColor,
        dpiScale
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.45f,0.625f)),
        ImLerp(rect.Min,rect.Max,ImVec2(0.55f,0.625f)),
        guideColor,
        dpiScale
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.47f,0.75f)),
        ImLerp(rect.Min,rect.Max,ImVec2(0.53f,0.75f)),
        guideColor,
        dpiScale
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.48f,0.875f)),
        ImLerp(rect.Min,rect.Max,ImVec2(0.52f,0.875f)),
        guideColor,
        dpiScale
      );

      dl->AddLine(
        ImLerp(rect.Min,rect.Max,ImVec2(0.5f,0.08f)),
        ImLerp(rect.Min,rect.Max,ImVec2(0.5f,0.92f)),
        guideColor,
        dpiScale
      );

      oscWidth=round(inRect.Max.x-inRect.Min.x);
      if (oscWidth<1) oscWidth=1;
      if (oscWidth>1024) oscWidth=1024;

      ImDrawListFlags prevFlags=dl->Flags;
      if (!settings.oscAntiAlias) {
        dl->Flags&=~(ImDrawListFlags_AntiAliasedLines|ImDrawListFlags_AntiAliasedLinesUseTex);
      }

      if (settings.oscMono) {
        for (int i=0; i<oscWidth; i++) {
          float x=(float)i/(float)oscWidth;
          float avg=0;
          for (int j=0; j<e->getAudioDescGot().outChans; j++) {
            avg+=oscValues[j][i];
          }
          avg/=e->getAudioDescGot().outChans;

          float y=avg*oscZoom;
          if (!settings.oscEscapesBoundary) {
            if (y<-0.5f) y=-0.5f;
            if (y>0.5f) y=0.5f;
          }
          waveform[i]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y));
        }
        
        if (settings.oscEscapesBoundary) {
          dl->PushClipRectFullScreen();
          dl->AddPolyline(waveform,oscWidth,color,ImDrawFlags_None,dpiScale);
          dl->PopClipRect();
        } else {
          dl->AddPolyline(waveform,oscWidth,color,ImDrawFlags_None,dpiScale);
        }
      } else {
        for (int ch=0; ch<e->getAudioDescGot().outChans; ch++) {
          for (int i=0; i<oscWidth; i++) {
            float x=(float)i/(float)oscWidth;
            float y=oscValues[ch][i]*oscZoom;
            if (!settings.oscEscapesBoundary) {
              if (y<-0.5f) y=-0.5f;
              if (y>0.5f) y=0.5f;
            }
            waveform[i]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y));
          }

          if (!isClipping) {
            color=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_WAVE_CH0+ch]);
          }
          
          if (settings.oscEscapesBoundary) {
            dl->PushClipRectFullScreen();
            dl->AddPolyline(waveform,oscWidth,color,ImDrawFlags_None,dpiScale);
            dl->PopClipRect();
          } else {
            dl->AddPolyline(waveform,oscWidth,color,ImDrawFlags_None,dpiScale);
          }
        }
      }

      dl->Flags=prevFlags;
      
      if (settings.oscBorder) {
        dl->AddRect(inRect.Min,inRect.Max,borderColor,settings.oscRoundedCorners?(8.0f*dpiScale):0.0f,0,1.5f*dpiScale);
      }
    }
    if (oscZoomSlider && ImGui::IsItemHovered()) {
      float val=20.0*log10(2.0*fabs(0.5-((ImGui::GetMousePos().y-inRect.Min.y)/(inRect.Max.y-inRect.Min.y))));
      if (val>0.0f) val=0.0f;
      if (val<=-INFINITY) {
        ImGui::SetTooltip("(-Infinity)dB");
      } else {
        ImGui::SetTooltip("%.1fdB",val);
      }
    }
    if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
      oscZoomSlider=!oscZoomSlider;
    }
  }
  if (settings.oscTakesEntireWindow) {
    ImGui::PopStyleVar(3);
  }
  if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_OSCILLOSCOPE;
  ImGui::End();
}
