#include <p_date.h>
#include <p_std.h>
#include <p_config.h>
#include <date.h>
#include <hwim.h>
#include <s_.rsg>
#include <mfne.g>
#include <dat.g>
#include <dat.rsg>

LOCAL_C VOID InitField(PR_DATE *self,UINT i,LONG val,LONG min,LONG max,UBYTE width)
    {
    self->mfne.f[i].value=val;
    self->mfne.f[i].limits[MFNE_LOWER]=min;
    self->mfne.f[i].limits[MFNE_UPPER]=max;
    self->mfne.f[i].width=width;
    }

LOCAL_C VOID DayMonthYear(PR_DATE *self,INT i,INT j,INT k)
    {
    self->date.day=i;
    self->date.month=j;
    self->date.year=k;
    }

#pragma METHOD_CALL

METHOD VOID date_wn_init(PR_DATE *self,VOID *par,PR_WIN *landlord)
    {
    P_DAYSEC ds;
    P_DATE dt;
    E_CONFIG cfg;

    p_getctd(&cfg);
    self->lodger.landlord=landlord;
    self->mfne.selected=FALSE;
    self->mfne.changed=FALSE;
    self->mfne.cField=0;
    self->mfne.nField=3;
    self->mfne.f[0].separator=cfg.dateSeparator;
    self->mfne.f[1].separator=cfg.dateSeparator;
    self->mfne.f[2].flags=MFNE_SUPPRESS_SEPARATOR;
    self->date.dType=cfg.dateType;
    if (self->date.dType==E_DATE_EUROPE)
        {
        DayMonthYear(self,0,1,2);
        }
    else if (self->date.dType==E_DATE_USA)
        {
        DayMonthYear(self,1,0,2);
        }
    else if (self->date.dType==E_DATE_JAPAN)
        {
        DayMonthYear(self,2,1,0);
        }
    InitField(self,self->date.day,1,1,31,2);
    InitField(self,self->date.month,1,1,12,2);
    InitField(self,self->date.year,1,1900,2154,4);
    dt.year=254;
    dt.month=11;
    dt.day=30;
    dt.hour=dt.minute=dt.second=dt.yrday=0;
    p_dttods(&dt,&ds);
    self->date.max=ds.day;
    }

METHOD VOID date_wn_set(PR_DATE *self,ULONG *par)
    {
    P_DAYSEC ds;
    P_DATE dt;

    ds.day=*par;
    ds.sec=0;
    if (ds.day>self->date.max)
        {
        ds.day=self->date.max;
        }
    p_dstodt(&ds,&dt);
    self->mfne.f[self->date.day].value=dt.day+1;
    self->mfne.f[self->date.month].value=dt.month+1;
    self->mfne.f[self->date.year].value=dt.year+1900;
    p_supersend2(self,O_WN_SET);
    }

METHOD VOID date_wn_sense(PR_DATE *self,ULONG *par)
    {
    P_DATE dt;
    P_DAYSEC ds;

    dt.second=0;
    dt.minute=0;
    dt.hour=0;
    dt.day=self->mfne.f[self->date.day].value-1;
    dt.month=self->mfne.f[self->date.month].value-1;
    dt.year=self->mfne.f[self->date.year].value-1900;
    p_dttods(&dt,&ds);
    *par=ds.day;
    }

METHOD INT date_lg_self_check(PR_DATE *self,INT can_defer)
    {
    INT ret;
    INT days;

    ret=p_supersend3(self,O_LG_SELF_CHECK,can_defer);
    if ((can_defer==FALSE)&&(ret==LG_CHECK_OK_CHANGED))
        {
        days=p_dayinm((INT)self->mfne.f[self->date.year].value-1900,
            (INT)self->mfne.f[self->date.month].value-1);
        if (self->mfne.f[self->date.day].value>(LONG)days)
            {
            hBeep();
            self->mfne.cField=self->date.day;
            self->mfne.f[self->date.day].value=(LONG)days;
            p_supersend3(self,O_LG_SELF_CHECK,TRUE);
            hInfoPrint(MONTH_DAY_RESET);
            ret=LG_CHECK_FAILED_CHANGED;
            }
        }
    return(ret);
    }
