#include "vdrstatus.h"
#ifdef VIDEOSTATUS
#include "videostatus.h"
#endif
#include "setup.h"
#include "i18n.h"

#ifdef VIDEOSTATUS
extern int fd_video;
#endif

cVdrStatus VdrStatus;

cVdrStatus::cVdrStatus(void) {
    for (int i = 0; i < MAXDEVICES; i++) {
	ChannelDevice[i] = NULL;
	ChannelNumber[i] = 0;
	RecordingName[i] = NULL;
	RecordingDevice[i] = NULL;
	RecordingNumber[i] = 0;
	}
    RecordingNumbers = 0;
    ReplayingName = NULL;
    AudioTrack = NULL;
    AudioChannel = NULL;
    Title = NULL;
    StatusMessage = NULL;
    HelpKeys[0] = NULL;
    HelpKeys[1] = NULL;
    HelpKeys[2] = NULL;
    HelpKeys[3] = NULL;
    CurrentItem = NULL;
    TextItem = NULL;
    Channel = NULL;
    PresentTitle = NULL;
    PresentSubtitle = NULL;
    FollowingTitle = NULL;
    FollowingSubtitle = NULL;
    }

cVdrStatus::~cVdrStatus() {
    for (int i = 0; i < MAXDEVICES; i++)
        if (RecordingName[i]) {
	    free(RecordingName[i]);
	    RecordingName[i] = NULL;
	    }
    if (ReplayingName) {
        free(ReplayingName);
        ReplayingName = NULL;
	}
    if (AudioTrack) {
        free(AudioTrack);
        AudioTrack = NULL;
	}
    if (AudioChannel) {
        AudioChannel = NULL;
	}
    if (Title) {
        free(Title);
        Title = NULL;
	}
    if (StatusMessage) {
        free(StatusMessage);
        StatusMessage = NULL;
	}
    if (HelpKeys[0]) {
        free(HelpKeys[0]);
        HelpKeys[0] = NULL;
	}
    if (HelpKeys[1]) {
        free(HelpKeys[1]);
        HelpKeys[1] = NULL;
	}
    if (HelpKeys[2]) {
        free(HelpKeys[2]);
        HelpKeys[2] = NULL;
	}
    if (HelpKeys[3]) {
        free(HelpKeys[3]);
        HelpKeys[3] = NULL;
	}
    if (CurrentItem) {
        free(CurrentItem);
        CurrentItem = NULL;
	}
    if (TextItem) {
        free(TextItem);
        TextItem = NULL;
	}
    if (Channel) {
        free(Channel);
        Channel = NULL;
	}
    if (PresentTitle) {
        free(PresentTitle);
        PresentTitle = NULL;
	}
    if (PresentSubtitle) {
        free(PresentSubtitle);
        PresentSubtitle = NULL;
	}
    if (FollowingTitle) {
        free(FollowingTitle);
        FollowingTitle = NULL;
	}
    if (FollowingSubtitle) {
        free(FollowingSubtitle);
        FollowingSubtitle = NULL;
	}
    }

/*cSkinElchiStatus::cSkinElchiStatus() {
    }

cSkinElchiStatus::~cSkinElchiStatus() {
    }*/
    
void cSkinElchiStatus::ChannelSwitch(const cDevice * Device, int ChannelNumber) {
    int i = Device->CardIndex();
    VdrStatus.ChannelDevice[i] = Device;
    VdrStatus.ChannelNumber[i] = ChannelNumber;
    if (!ChannelNumber && cDevice::ActualDevice()->CardIndex() == i) {
	if (VdrStatus.AudioTrack) {
    	    free(VdrStatus.AudioTrack);
    	    VdrStatus.AudioTrack = NULL;
	    }
	if (VdrStatus.AudioChannel) {
    	    VdrStatus.AudioChannel = NULL;
	    }
	}
#ifdef VIDEOSTATUS
    if (fd_video >= 0) {
	VideoStatus.Update();
	}
#endif
    }

#if VDRVERSNUM >= 10338
void cSkinElchiStatus::Recording(const cDevice *Device, const char *Name, const char *FileName, bool On) {
#else
void cSkinElchiStatus::Recording(const cDevice * Device, const char * Name) {
#endif
    int i = Device->CardIndex();
    VdrStatus.RecordingDevice[i] = Device;
    if (Name) {
	asprintf(&VdrStatus.RecordingName[i], "%s", Name);
	VdrStatus.RecordingNumber[i] += 1;
	VdrStatus.RecordingNumbers++;
	}
    else {
	if (VdrStatus.RecordingName[i]) {
    	    free(VdrStatus.RecordingName[i]);
            VdrStatus.RecordingName[i] = NULL;
	    }
	if (VdrStatus.RecordingNumber[i] > 0)
	    VdrStatus.RecordingNumber[i]--;
	if (VdrStatus.RecordingNumbers > 0)
	    VdrStatus.RecordingNumbers--;
	}
    }

#if VDRVERSNUM >= 10338
void cSkinElchiStatus::Replaying(const cControl *Control, const char *Name, const char *FileName, bool On) {
#else
void cSkinElchiStatus::Replaying(const cControl * Control, const char * Name) {
#endif
    if (Name)
	asprintf(&VdrStatus.ReplayingName,"%s",Name);
    else
	if (VdrStatus.ReplayingName) {
    	    free(VdrStatus.ReplayingName);
            VdrStatus.ReplayingName = NULL;
	    }
#ifdef VIDEOSTATUS
    if (fd_video >= 0) {
	VideoStatus.Update();
	}
#endif
    }

void cSkinElchiStatus::SetVolume(int Volume, bool Absolute) {
    VdrStatus.VolumeChange++;
    VdrStatus.Volume = Volume;//vdr 1.3.22 -> get volume always absolute ???
    }

void cSkinElchiStatus::SetAudioTrack(int Index, const char * const *Tracks) {
    if (Tracks) {
	VdrStatus.AudioTrackIndex = Index + 1;
	VdrStatus.Tracks = Tracks;
	asprintf(&VdrStatus.AudioTrack,"%s", Tracks[Index]);
	}
    else
	if (VdrStatus.AudioTrack) {
    	    VdrStatus.AudioTrackIndex = 0;
	    VdrStatus.Tracks = NULL;
	    free(VdrStatus.AudioTrack);
            VdrStatus.AudioTrack = NULL;
	    }
    }

void cSkinElchiStatus::SetAudioChannel(int AudioChannel) {
    switch (AudioChannel) {
	case -1: VdrStatus.AudioChannel = tr("Digital"); break;
	case 0:	VdrStatus.AudioChannel = tr("Stereo"); break;
	case 1: VdrStatus.AudioChannel = tr("Left channel"); break;
	case 2:	VdrStatus.AudioChannel = tr("Right channel"); break;
	default: VdrStatus.AudioChannel = NULL;
	}
    }

void cSkinElchiStatus::OsdClear() {
    }

void cSkinElchiStatus::OsdTitle(const char * Title) {
    if (Title)
	asprintf(&VdrStatus.Title,"%s", Title);
    else
	if (VdrStatus.Title) {
    	    free(VdrStatus.Title);
            VdrStatus.Title = NULL;
	    }
    }

void cSkinElchiStatus::OsdStatusMessage(const char * Message) {
    if (Message)
	asprintf(&VdrStatus.StatusMessage,"%s", Message);
    else
	if (VdrStatus.StatusMessage) {
    	    free(VdrStatus.StatusMessage);
            VdrStatus.StatusMessage = NULL;
	    }
    }

void cSkinElchiStatus::OsdHelpKeys(const char * Red, const char * Green, const char * Yellow, const char * Blue) {
    if (Red)
	asprintf(&VdrStatus.HelpKeys[0],"%s", Red);
    else
	if (VdrStatus.HelpKeys[0]) {
    	    free(VdrStatus.HelpKeys[0]);
            VdrStatus.HelpKeys[0] = NULL;
	    }
    if (Green)
	asprintf(&VdrStatus.HelpKeys[1],"%s", Green);
    else
	if (VdrStatus.HelpKeys[1]) {
    	    free(VdrStatus.HelpKeys[1]);
            VdrStatus.HelpKeys[1] = NULL;
	    }
    if (Yellow)
	asprintf(&VdrStatus.HelpKeys[2],"%s", Yellow);
    else
	if (VdrStatus.HelpKeys[2]) {
    	    free(VdrStatus.HelpKeys[2]);
            VdrStatus.HelpKeys[2] = NULL;
	    }
    if (Blue)
	asprintf(&VdrStatus.HelpKeys[3],"%s", Blue);
    else
	if (VdrStatus.HelpKeys[3]) {
    	    free(VdrStatus.HelpKeys[3]);
            VdrStatus.HelpKeys[3] = NULL;
	    }
    }

/* to slow in channels-menu, because it is call for all channels
void cSkinElchiStatus::OsdItem(const char * Text, int Index) {
    }*/

void cSkinElchiStatus::OsdCurrentItem(const char * Text) {
    if (Text)
	asprintf(&VdrStatus.CurrentItem,"%s", Text);
    else
	if (VdrStatus.CurrentItem) {
    	    free(VdrStatus.CurrentItem);
            VdrStatus.CurrentItem = NULL;
	    }
    }

void cSkinElchiStatus::OsdTextItem(const char * Text, bool Scroll) {
    if (Text)
	asprintf(&VdrStatus.TextItem,"%s", Text);
    else
	if (VdrStatus.TextItem) {
    	    free(VdrStatus.TextItem);
            VdrStatus.TextItem = NULL;
	    }
    VdrStatus.Scroll = Scroll;
    }

void cSkinElchiStatus::OsdChannel(const char * Text) {
    if (Text)
	asprintf(&VdrStatus.Channel,"%s", Text);
    else
	if (VdrStatus.Channel) {
    	    free(VdrStatus.Channel);
            VdrStatus.Channel = NULL;
	    }
    }

void cSkinElchiStatus::OsdProgramme(time_t PresentTime, const char * PresentTitle, const char * PresentSubtitle, time_t FollowingTime, const char * FollowingTitle, const char * FollowingSubtitle) {
    VdrStatus.PresentTime = PresentTime;
    if (PresentTitle)
	asprintf(&VdrStatus.PresentTitle,"%s", PresentTitle);
    else
	if (VdrStatus.PresentTitle) {
    	    free(VdrStatus.PresentTitle);
            VdrStatus.PresentTitle = NULL;
	    }
    if (PresentSubtitle)
	asprintf(&VdrStatus.PresentSubtitle,"%s", PresentSubtitle);
    else
	if (VdrStatus.PresentSubtitle) {
    	    free(VdrStatus.PresentSubtitle);
            VdrStatus.PresentSubtitle = NULL;
	    }
    VdrStatus.FollowingTime = FollowingTime;
    if (FollowingTitle)
	asprintf(&VdrStatus.FollowingTitle,"%s", FollowingTitle);
    else
	if (VdrStatus.FollowingTitle) {
    	    free(VdrStatus.FollowingTitle);
            VdrStatus.FollowingTitle = NULL;
	    }
    if (FollowingSubtitle)
	asprintf(&VdrStatus.FollowingSubtitle,"%s", FollowingSubtitle);
    else
	if (VdrStatus.FollowingSubtitle) {
    	    free(VdrStatus.FollowingSubtitle);
            VdrStatus.FollowingSubtitle = NULL;
	    }
    }
