/*
 * Emunation Plugin for MD-API 
 *
 * This code 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 code 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */
 
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "logger.h"

//For HexStr
#include "misc.h"

#include "main.h"
extern class DllClass m_DLLInstance;

const int cLogger::ECM_Text=0x01;
const int cLogger::ECM_Bin=0x02;
const int cLogger::EMM_Text=0x03;
const int cLogger::EMM_Bin=0x04;
const int cLogger::DCW=0x05;

cLogger::cLogger()
:m_IsBinECMActive(false),m_IsBinEMMActive(false),m_IsCWActive(false),
m_IsTextECMActive(false),m_IsTextEMMActive(false)
{
    ResetChChanged();
    WriteStartMessage();
}

void cLogger::LogData(unsigned char* data,int len,int type)
{
    if(!GetActive(type)) return;
    char filename[100];
    char timestr[50];
    GetTimeStr(timestr);
    GetFileName(filename,type);
	char LogPath[_MAX_PATH];
	sprintf(LogPath,"%s\\%s",m_DLLInstance.GetLogPath(),filename);
    FILE* f= fopen (LogPath,IsBinary(type)?"atb":"at");
    if (IsBinary(type)&&f)
    {
        fwrite(data,len,1,f);
    }

    if (!IsBinary(type)&&f)
    {
        char* txtdata= new char [len*2+1];
        HexStr(txtdata,data,len);

        if(IsTypeChChanged(type))
        {
           if (type == DCW)
               fprintf (f,"\n\nCh: %s \n",m_ch_name);
            else 
               fprintf (f,"\n\nCh: %s %s PID : 0x%.4X (%d)\n",m_ch_name,type==ECM_Text?"ECM":"EMM",type==ECM_Text?m_ECMPid:m_EMMPid,type==ECM_Text?m_ECMPid:m_EMMPid);

            SetChChanged(type);
        }

        fprintf(f,"%s  %s\n",timestr,txtdata);
        delete[] txtdata;
    }
    if(f)
        fclose(f);
}

bool cLogger::SetActive(bool flag,int type)
{
    switch (type)
    {
        case ECM_Text:
            m_IsTextECMActive=flag;
        break;
        case ECM_Bin:
            m_IsBinECMActive=flag;
        break;
        case EMM_Text:
            m_IsTextEMMActive=flag;
        break;
        case EMM_Bin:
            m_IsBinEMMActive=flag;
        break;
        case DCW:
            m_IsCWActive=flag;
        break;

    };
    return flag;
}

bool cLogger::GetActive(int type)
{
    switch (type)
    {
        case ECM_Text:
               return m_IsTextECMActive;
        break;
        case ECM_Bin:
            return m_IsBinECMActive;
        break;
        case EMM_Text:
            return m_IsTextEMMActive;
        break;
        case EMM_Bin:
            return m_IsBinEMMActive;
        break;
        case DCW:
            return m_IsCWActive;
        break;

    };
    return false;
}
char* cLogger::GetFileName (char* filename,int type)
{
    switch (type)
    {
        case ECM_Text:
               strcpy(filename,ECMTXTFILENAME);
        break;
        case ECM_Bin:
               strcpy(filename,ECMBINFILENAME);
        break;
        case EMM_Text:
               strcpy(filename,EMMTXTFILENAME);
        break;
        case EMM_Bin:
               strcpy(filename,EMMBINFILENAME);
        break;
        case DCW:
               strcpy(filename,DCWFILENAME);
        break;

    };
    return filename;  
}
bool cLogger::IsBinary(int type)
{
    return type==ECM_Bin||type==EMM_Bin;
}
bool cLogger::ToggleActive(int type)
{
    return SetActive(!GetActive(type),type);
}

void cLogger::OnChannelChange(char* chname,int ecmpid,int emmpid)
{
    strcpy(m_ch_name,chname);
    m_ECMPid=ecmpid;
    m_EMMPid=emmpid;
    ResetChChanged();
}

void cLogger::OnChannelChange(int ecmpid,int emmpid)
{
    m_ECMPid=ecmpid;
    m_EMMPid=emmpid;
    ResetChChanged();
}
bool cLogger::IsTypeChChanged(int type)
{
    switch (type)
    {
        case ECM_Text:
               return m_chchanged_ecm;
        break;
        case EMM_Text:
            return m_chchanged_emm;
        break;
        case DCW:
            return m_chchanged_cw;
        break;

    };
    return false;
}
void cLogger::ResetChChanged()
{
    m_chchanged_cw=true;
    m_chchanged_ecm=true;
    m_chchanged_emm=true;
}

bool cLogger::SetChChanged(int type)
{
    switch (type)
    {
        case ECM_Text:
            m_chchanged_ecm=false;
        break;

        case EMM_Text:
            m_chchanged_emm=false;
        break;

        case DCW:
            m_chchanged_cw=false;
        break;

    };
return true;
}

void cLogger::WriteStartMessage()
{ 
}

char* cLogger::GetTimeStr(char* str)
{
    time_t t;
    tm* lctime;
    time (&t);
    lctime = localtime (&t);
    sprintf (str,"[%.2d/%.2d/%.2d | %.2d:%.2d:%.2d]",lctime->tm_mon,lctime->tm_mday,lctime->tm_year+1900,lctime->tm_hour,lctime->tm_min,lctime->tm_sec);
 return NULL;   
}



