/*
 * whitecalibration.c: White calibration for the Atmolight-plugin
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <vdr/plugin.h>

#include "defs.h"
#include "setup.h"
#include "whitecalibration.h"

#define OSD_HEIGHT  575
#define OSD_WIDTH   704
#define TEXT_HEIGHT 40

#define BCOLOR      clrBlack
static int FCOLOR = 0xFFFFFFFF;

extern cPlugin *AtmoPlugin;

// --- cAtmoWhiteCalibration --------------------------------------------------

#define WHITE_CALIBRATION_RED   "White_Calibration_Red"
#define WHITE_CALIBRATION_GREEN "White_Calibration_Green"
#define WHITE_CALIBRATION_BLUE  "White_Calibration_Blue"

cAtmoWhiteCalibration::cAtmoWhiteCalibration(void)
{
  osd = NULL;
  cursor = 1;
  max_lines = 3;

  old_Mode = AtmoSetup.Mode;
  old_FilterMode = AtmoSetup.Filter_Mode;
  
  AtmoSetup.Mode = live_picture_hsv;
  AtmoSetup.Filter_Mode = no_filter;
}

cAtmoWhiteCalibration::~cAtmoWhiteCalibration()
{
  AtmoSetup.Mode = old_Mode;
  AtmoSetup.Filter_Mode = old_FilterMode;
  delete osd;
}

void cAtmoWhiteCalibration::AdjustValue(unsigned char *value, int step)
{
  if (( *value+step >= 0) && (*value+step <= 255)) { *value += step; }
}

void cAtmoWhiteCalibration::Adjust(int step)
{
  switch (cursor)
  {
    case 1: AdjustValue(&AtmoSetup.WhiteCalibration.r, step); break;
    case 2: AdjustValue(&AtmoSetup.WhiteCalibration.g, step); break;
    case 3: AdjustValue(&AtmoSetup.WhiteCalibration.b, step); break;
  }
}

void cAtmoWhiteCalibration::DrawLine(int line_nr, const char* desc, int value)
{
  int fc, bc;
  char tmp[25];
  int vert_start = (OSD_HEIGHT - (max_lines * TEXT_HEIGHT)) / 2 - TEXT_HEIGHT;

  if (cursor == line_nr) { bc = BCOLOR; fc = FCOLOR; } else { bc = FCOLOR; fc = BCOLOR; }

  sprintf(tmp, "%-15s", desc);
  osd->DrawText(OSD_WIDTH/2 - 80, vert_start + line_nr * TEXT_HEIGHT, tmp, fc, bc, cFont::GetFont(fontOsd));

  sprintf(tmp, "%15i", value);
  osd->DrawText(OSD_WIDTH / 2, vert_start + line_nr * TEXT_HEIGHT, tmp, fc, bc, cFont::GetFont(fontOsd));
}

void cAtmoWhiteCalibration::DrawTexts(void)
{
  if (!osd) { return; }

  switch (cursor) 
  {
    case 1:
    case 2:
    case 3: FCOLOR = 0xFFFFFFFF; break;
  }

  osd->DrawRectangle(0, 0, 703, 575, FCOLOR);

  DrawLine(1, tr("red"),   AtmoSetup.WhiteCalibration.r);
  DrawLine(2, tr("green"), AtmoSetup.WhiteCalibration.g);
  DrawLine(3, tr("blue"),  AtmoSetup.WhiteCalibration.b);
}

void cAtmoWhiteCalibration::Show(void)
{
  osd = cOsdProvider::NewOsd(0, 0);
  if (osd)
  {
    tArea Area = { 0, 0, 703, 575, 1 };
    osd->SetAreas(&Area, 1);
    DrawTexts();
    osd->Flush();
  }
}

bool cAtmoWhiteCalibration::SetupParse(const char *Name, const char *Value)
{
       if (!strcasecmp(Name, WHITE_CALIBRATION_RED))   AtmoSetup.WhiteCalibration.r = atoi(Value);
  else if (!strcasecmp(Name, WHITE_CALIBRATION_GREEN)) AtmoSetup.WhiteCalibration.g = atoi(Value);
  else if (!strcasecmp(Name, WHITE_CALIBRATION_BLUE))  AtmoSetup.WhiteCalibration.b = atoi(Value);
  else return false;

  return true;
}

void cAtmoWhiteCalibration::Store(void)
{
  AtmoPlugin->SetupStore(WHITE_CALIBRATION_RED,   AtmoSetup.WhiteCalibration.r);
  AtmoPlugin->SetupStore(WHITE_CALIBRATION_GREEN, AtmoSetup.WhiteCalibration.g);
  AtmoPlugin->SetupStore(WHITE_CALIBRATION_BLUE,  AtmoSetup.WhiteCalibration.b);
}

eOSState cAtmoWhiteCalibration::ProcessKey(eKeys Key)
{
  eOSState state = cOsdObject::ProcessKey(Key);

  if (state == osUnknown)
  {
    switch (Key & ~k_Repeat)
    {
      case kUp:     if (cursor > 1)         { cursor--; } break;
      case kDown:   if (cursor < max_lines) { cursor++; } break; 

      case kLeft:   Adjust( -1); break;
      case kRight:  Adjust(  1); break;
      case k1:      Adjust( -5); break;
      case k3:      Adjust(  5); break;
      case k4:      Adjust(-25); break;
      case k6:      Adjust( 25); break;
      case k7:      Adjust(-50); break;
      case k9:      Adjust( 50); break;

      case kOk:     AtmoSetup.DoWhiteCalibration = false; Store(); return osEnd; break;
      // case kBack:   AtmoSetup.DoWhiteCalibration = false; return osEnd; break;
      default:      return state; break;
    }
    DrawTexts();
    osd->Flush();
    state = osContinue;
  }
  return state;
}
