#include "i18n.h"
#include "DisplayVolume.h"
#include "setup.h"
#if VDRVERSNUM >= 10332
#include <vdr/device.h>
#endif
#ifdef VIDEOSTATUS
#include "videostatus.h"
#endif
#include "symbols/mute.xpm"
#include "symbols/volume.xpm"

extern bool vdrosd;
// --- cSkinElchiDisplayVolume ---------------------------------------------
cBitmap cSkinElchiDisplayVolume::bmMute(mute_xpm);
cBitmap cSkinElchiDisplayVolume::bmVolume(volume_xpm);

cSkinElchiDisplayVolume::cSkinElchiDisplayVolume(void)
{
    noflush = true;
#ifdef VIDEOSTATUS
    VideoStatus.Update();
#endif
    changed = message = false;
    font = cFont::GetFont(fontOsd);
    lh = font->Height();
    lhh = lh / 2;
#ifdef VIDEOSTATUS
    if (SkinElchiSetup.videostatus)
	vdrosd = (VideoStatus.NTSC() == (SkinElchiSetup.videosystem == vsNTSC));
    else
#endif
	vdrosd = (cDevice::PrimaryDevice()->GetVideoSystem() == vsNTSC) == (SkinElchiSetup.videosystem == vsNTSC);
    osd = cOsdProvider::NewOsd(vdrosd ? Setup.OSDLeft : SkinElchiSetup.osdleft, (vdrosd ? Setup.OSDTop : SkinElchiSetup.osdtop) + (vdrosd ? Setup.OSDHeight : SkinElchiSetup.osdheight) - 2 * lh);
    tArea Areas[] = { { 0, 0, (vdrosd ? Setup.OSDWidth : SkinElchiSetup.osdwidth) - 1, 2 * lh - 1, 4 } };
    osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
    mute = -1;
    current = -1;
    MessageScroll.DestWidth = vdrosd ? Setup.OSDWidth : SkinElchiSetup.osdwidth;
    MessageScroll.Font = font;
    MessageScroll.xOff = 0;
    MessageScroll.yOff = lh;
    MessageScroll.delay = 20;
    MessageScroll.intervall = 7;
    noflush = false;
}

cSkinElchiDisplayVolume::~cSkinElchiDisplayVolume()
{
  changed = message = true;
  delete osd;
}

void cSkinElchiDisplayVolume::SetVolume(int Current, int Total, bool Mute) {
    if (Mute) {
    	if (mute != int(Mute)){
	    changed = true;
	    tColor tr = Theme.Color(Transparent);
    	    osd->DrawRectangle(0, lh, osd->Width() - 1, osd->Height() - 1, tr);
	    char *Prompt;
    	    asprintf(&Prompt,"%03d", Current);
    	    int l = font->Width(Prompt) + lhh;
    	    osd->DrawText(lhh + 3, lh + 1, Prompt, Theme.Color(clrBackground), tr, font);
    	    osd->DrawText(lhh, lh - 2, Prompt, Theme.Color(clrVolumePrompt), tr, font);
    	    free(Prompt);
    	    osd->DrawBitmap(l + 3, lh + (lh - bmMute.Height()) / 2, bmMute, Theme.Color(clrVolumeSymbolMuteFg), Theme.Color(clrVolumeSymbolMuteBg));
	    mute = (int)Mute;
	    }
	}
    else {
	int w = osd->Width();
	int h = osd->Height();
	if (mute != int(Mute)){
	    changed = true;
	    tColor tr = Theme.Color(Transparent);
	    tColor vbl = Theme.Color(clrVolumeBarLower);
	    int w1 = w - 1;
	    int h1 = h - 1;
    	    osd->DrawRectangle(0, lh, w1, h1, tr);
    	    osd->DrawEllipse(0, lh, lhh - 1, h1, vbl, 7);
    	    osd->DrawRectangle(lhh, lh, w1 - lhh, h1, vbl);
    	    osd->DrawEllipse(w - lhh, lh, w1, h1, vbl, 5);
    	    osd->DrawRectangle(0, 0, w1, lh - 1, tr);
    	    const char *Prompt = tr("Volume ");
    	    int l = font->Width(Prompt) + lhh;
    	    osd->DrawText(lhh + 3, 1, Prompt, Theme.Color(clrBackground), tr, font);
    	    osd->DrawText(lhh, -2, Prompt, Theme.Color(clrVolumePrompt), tr, font);
    	    osd->DrawBitmap(l + 3, (lh - bmVolume.Height()) / 2, bmVolume, Theme.Color(clrVolumeSymbolVolumeFg), tr);
	    mute = (int)Mute;
	    }
	if (current != Current){
	    changed = true;
    	    int p = (w - lh) * Current / Total;
	    int h5 = h - 5;
	    int lh4 = lh + 4;
	    int lhhp = lhh + p;
    	    osd->DrawRectangle(lhh, lh4, lhhp - 1, h5, Theme.Color(clrVolumeBarUpper));
	    osd->DrawRectangle(lhhp, lh4, w - lhh - 1, h5, Theme.Color(clrVolumeBarLower));
    	    current = Current;
	    }
        }
    }

void cSkinElchiDisplayVolume::SetMessage(eMessageType Type, const char *Text) {
#ifdef DEBUG
    isyslog("skinelchi: cSkinElchiDisplayVolume::SetMessage(%d,%s)", (int)Type, Text);
#endif
    if (Text) {
	char *text;
	asprintf(&text, "%s", Text);
	if (!strcasecmp(text, tr("Going to sleep in about one minute"))) {
	    asprintf(&text, "\"%s\" <- Sleeptimer-Message received -> not printed on screen", Text);
	    esyslog(text);
	    }
	else {
	    message = changed = true;
	    MessageScroll.Fg = Theme.Color(clrMessageStatusFg + 2 * Type);
	    MessageScroll.Bg = Theme.Color(clrMessageStatusBg + 2 * Type);
	    osd->SaveRegion(0, 0, (vdrosd ? Setup.OSDWidth : SkinElchiSetup.osdwidth) - 1, 2 * lh - 1);
	    osd->DrawEllipse(0, lh, lhh - 1, 2 * lh - 1, MessageScroll.Bg, 7);
	    if (MessageScroll.SetText(Text)) {
		MessageScroll.pos = 0;
		MessageScroll.dir = -1;
		MessageScroll.Time.Set();
		osd->DrawText(MessageScroll.xOff, MessageScroll.yOff, MessageScroll.Text, MessageScroll.Fg, MessageScroll.Bg, MessageScroll.Font, MessageScroll.DestWidth);
		}
	    else {
		MessageScroll.dir = 0;
		osd->DrawText(MessageScroll.xOff, MessageScroll.yOff, MessageScroll.Text, MessageScroll.Fg, MessageScroll.Bg, MessageScroll.Font, MessageScroll.DestWidth, 0, taCenter);
		}
	    osd->DrawEllipse((vdrosd ? Setup.OSDWidth : SkinElchiSetup.osdwidth) - lhh, lh, (vdrosd ? Setup.OSDWidth : SkinElchiSetup.osdwidth) - 1, 2 * lh - 1, MessageScroll.Bg, 5);
	    }
	free(text);
	}
    else {
	if (message) {
	    changed = true;
	    MessageScroll.dir = 0;
	    message = false;
	    osd->RestoreRegion();
	    }
	}

    }

void cSkinElchiDisplayVolume::Flush(void) {
    if (!noflush) {
	noflush = true;
	if (changed) {
#ifdef DEBUG
	    isyslog("skinelchi: cSkinElchiDisplayVolume::Flush()");
#endif	
	    osd->Flush();
	    changed = false;
	    }
	else {
	    if (MessageScroll.dir) {
		if (MessageScroll.DrawNeeded()) {
#ifdef DEBUG
	    	    isyslog("skinelchi: cSkinElchiDisplayVolume::(Flush) because message is show");
#endif
		    for (int i = 0; i < MessageScroll.DestWidth; i++) {
			for (int j = 0; j < MessageScroll.Height(); j++)
		    	    osd->DrawPixel(MessageScroll.xOff + i, MessageScroll.yOff + j, MessageScroll.bmp->Color(*(MessageScroll.bmp->Data(i + MessageScroll.pos, j))));
			}
		    osd->Flush();
		    }
		}
#ifdef DEBUG
	    else
		isyslog("skinelchi: skip cSkinElchiDisplayVolume::Flush()");
#endif
	    }
	noflush = false;
	}
#ifdef DEBUG
    else
	isyslog("skinelchi: skip cSkinElchiDisplayVolume::Flush() because cSkinElchiDisplayTracks::Flush() is active");
#endif
    }
