/*
 * advchctrlchannel.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <ctype.h>
#include <stdlib.h>
#include <vdr/device.h>
#include "advchctrlchannel.h"
#include "advchctrlconfig.h"
#include "advchctrlcommon.h"

// The volume channels list (stored on disk)
cAdvChCtrlChannels AdvChCtrlChannelsList;

int number=0;
int current=0;
tChannelID VolCtrlChannels[CHANNELSMAX];

bool cAdvChCtrlChannel::Parse(char *s) 
{
    DEBUGVOL ("cAVolCtrlChannel::Parse <%s>", s);
    //let's use the tChannelID type
    Index = tChannelID::FromString(s);
    char *p = strchr ( s, ':');
    if (p) 
    {
	const char *Name = compactspace(p + 1);
	DEBUGVOL ("cAdvChCtrlChannel::Parse()  Name:<%s>", Name);     
	p = strchr(Name, '=');
	if (p)
	{
    	    *p = 0;
	    const char* Value = compactspace(p + 1);
	    // todo check if Name == "VOL"
	    Volume = atoi(Value);
	    DEBUGVOL ("cAdvChCtrlChannel::Parse Name <%s> Value:<%d>", Name, Volume);
	}
    }
    if ( Index.Valid() )
    {
	// Maybe control the uniqueness...
	return true;
    } else 
	return false;
}

bool cAdvChCtrlChannel::Save(FILE *f)
{
    /* Fix crash problem reported by Manuel Hartl    */
    /* when loading a non existing prefered channel  */
    cChannel *channel;
    channel = Channels.GetByChannelID(GetIndex(), true);
    if (!channel) 
    {
	isyslog("Volume channel '%s' does not exist anymore. Removing\n", *(Index.ToString()) );
	return true;
    }
    if (!(AdvChCtrlChannelsList.channel_to_remove == GetIndex()))
	return fprintf(f, "%s:VOL=%03d\n", *(Index.ToString()), Volume ) > 0;
    else
	return true;
}

int cAdvChCtrlChannel::GetVolume(tChannelID index)
{
    return Volume;
}

tChannelID cAdvChCtrlChannels::channel_to_remove = tChannelID::InvalidID;

bool cAdvChCtrlChannels::Load(const char *filename, bool dummy)
{
    FileName_ = filename;
    if(cConfig<cAdvChCtrlChannel>::Load(filename,true)) 
    {
	return true;
    }
    return false;
}

bool cAdvChCtrlChannels::Save(void)
{
    if (cConfig<cAdvChCtrlChannel>::Save()) 
    {
	// isyslog("saved setup to %s", FileName());
	channel_to_remove = tChannelID::InvalidID;
	//cConfig<cPreferedChannel>::Load(FileName_,true);
	return true;
    }
    return false;
}

cAdvChCtrlChannel *cAdvChCtrlChannels::GetAVolCtrlChannelNumber(int number)
{
    if(number >= CHANNELSMAX)
	return NULL;
    cAdvChCtrlChannel *l = First();
    for (int i=0; i<number; i++)
	l = Next(l);
    return l;
}

void cAdvChCtrlChannels::AddAVolCtrlChannel(tChannelID Index, int Volume, int DefaultVolume )
{
    for (cAdvChCtrlChannel *channel = First(); channel; channel = Next(channel))
    {
	if (channel->GetIndex() == Index)
	{
	    // check if volume has change
	    if (channel->GetVolume(Index) == Volume) return;
	    channel->SetVolume(Volume);
    	    DEBUGVOL ("cAdvChCtrlChannels::AddAVolCtrlChannel: Channel exists, chaged only volume to <%d>", Volume);
    	    Save();
    	    return;
	}
    }
    if (Volume == DefaultVolume) return;
    // only if we have different Volume than DefaultVolume => Add to List
    cAdvChCtrlChannel *VolumeChannel = new cAdvChCtrlChannel;
    VolumeChannel->SetIndex(Index);
    VolumeChannel->SetVolume(Volume);
    Add(VolumeChannel);
    DEBUGVOL ("cAdvChCtrlChannels::AddAVolCtrlChannel <%s><%d>", *(Index.ToString()), Volume );
    Save();
}

void cAdvChCtrlChannels::RemoveAVolCtrlChannel(tChannelID removenumber)
{
    for (cAdvChCtrlChannel *channel = First(); channel; channel = Next(channel)) 
    {
	if (channel->GetIndex() == removenumber) Del(channel, false);
    }
    channel_to_remove = removenumber;
    Save();
}

int cAdvChCtrlChannels::GetVolume(tChannelID Index)
{
    for (cAdvChCtrlChannel *channel = First(); channel; channel = Next(channel)) 
    {
	if (channel->GetIndex() == Index)
	{
	    int myVolume = channel->GetVolume(Index);
       
    	    if (myVolume == advchctrlConfig.defaultvolume) return advchctrlConfig.defaultvolume;
    	    DEBUGVOL ("cAdvChCtrlChannels::GetVolume() Channel  volume  <%d>", myVolume);
    	    return myVolume;
	}
    }
    return -1; 
}

