unit FixPhotocell;

  interface
    uses
      AdsData  (* used by AdsFunc *),
      AdsFunc  (* used by AdsTable *),
      AdsTable (* tAdsQuery *),
      Classes  (**),
      ComCtrls (* tDateTimePicker *),
      Controls (* tDate *),
      DB       (* used by AdsTable *),
      ExtCtrls (* tShape *),
      Forms    (* tForm *),
      StdCtrls (* tButton, tGroupBox, tLabel, tListBox *),
      Windows  (**);

    type
      tfmFixPhotocell = class(tForm)
          qSunriseSunset: tAdsQuery;
          dtpMorningAfter: tDateTimePicker;
          grpAction: tGroupBox;
            lbxAction: tListBox;
          grpBetterMorning: tGroupBox;
            dtpBetterMorning:     tDateTimePicker;
            dtpBetterSunrise:     tDateTimePicker;
            dtpBetterSunsetTime:  tDateTimePicker;
            dtpPreviousSunsetDay: tDateTimePicker;
            lbBetterMorningDay: tLabel;
            lbPreviousSunset:   tLabel;
            lbSunrise:          tLabel;
            shapeDoItAlert: tShape;
              btnDoIt: tButton;
                qFixPhotocell: tAdsQuery;
          lbMorningAfter: tLabel;
          
          procedure btnDoItClick          (Sender: tObject);
          procedure dtpBetterMorningChange(Sender: tObject);
          procedure FormDblClick          (Sender: tObject);
          procedure FormShow              (Sender: tObject);
        end (* tfmFixPhotocell *);

    var
      fmFixPhotocell: tfmFixPhotocell;

  implementation
    uses
      DateUtils              (* DecodeDateTime, IncDay, RecodeTime, Today *),
      HC3DM                  (* dmHC3 *),
      HC3Utilities           (* LastUIDOf *),
      HC4Main                (* fmHC4Main *),
      MyConsts               (* UsualFormColor *),
      RRF_StringAndTimeUtils (* LocalToUTC *),
      SummaryManager         (* fmSummary *),
      SysUtils               (* Format, FormatDateTime *);

      {$R *.dfm}

    var
      DeleteStart, DeleteStop, FirstSecond, LastSecond: tDateTime;

    procedure tfmFixPhotocell.btnDoItClick(Sender: tObject);
      (* clean up the table *)

      const
        OutsideUID = 13;

      var
        NewUID_1: integer;
    
      begin (* tfmFixPhotocell.btnDoItClick *)
        NewUID_1 := LastUIDOf('Light') + 1;

        with qFixPhotocell do
          begin (* qFixPhotocell *)
            ParamByName('DeleteStart'   ).AsDateTime := DeleteStart;
            ParamByName('DeleteStartUTC').AsDateTime := LocalToUTC(DeleteStart);
            ParamByName('DeleteStop'    ).AsDateTime := DeleteStop;
            ParamByName('DeleteStopUTC' ).AsDateTime := LocalToUTC(DeleteStop);
            ParamByName('FirstSecond'   ).AsDateTime := FirstSecond;
            ParamByName('FirstSecondUTC').AsDateTime := LocalToUTC(FirstSecond);
            ParamByName('LastSecond'    ).AsDateTime := LastSecond;
            ParamByName('LastSecondUTC' ).AsDateTime := LocalToUTC(LastSecond);
            ParamByName('OutsideUID'    ).AsInteger  := OutsideUID;
            ParamByName('UID_1'         ).AsInteger  := NewUID_1;
            ParamByName('UID_2'         ).AsInteger  := NewUID_1 + 1;
            ParamByName('UID_3'         ).AsInteger  := NewUID_1 + 2;
            ParamByName('UID_4'         ).AsInteger  := NewUID_1 + 3;
            ExecSQL
          end   (* qFixPhotocell *);

        fmSummaryManager.Show
      end   (* tfmFixPhotocell.btnDoItClick *);

    procedure tfmFixPhotocell.dtpBetterMorningChange(Sender: tObject);
      (* get sunset/sunrise data *)

      var
        BadSunrise, BadSunset, MorningDay: tDateTime;
        Day, Hour, Millisecond, Minute, Month, Second, Year: word;

      procedure AddStep(const S: string);
        (* save keystrokes *)
      
        begin (* AddStep *)
          with lbxAction do
            ItemIndex := Items.Add(S)
        end   (* AddStep *);

      begin (* tfmFixPhotocell.dtpBetterMorningChange *)
        BadSunrise := dtpMorningAfter.Date;
        BadSunset := IncDay(BadSunrise, -1);
        MorningDay := dtpBetterMorning.Date;
        dtpPreviousSunsetDay.Date := IncDay(MorningDay, -1);
        with qSunriseSunset do
          begin (* qSunriseSunset *)
            Close;
            ParamByName('TheDate').AsDate := MorningDay;
            Open;
            dtpBetterSunrise.Time := FieldByName('Sunrise').AsDateTime;
            Close;
            ParamByName('TheDate').AsDate := dtpPreviousSunsetDay.Date;
            Open;
            dtpBetterSunsetTime.Time := FieldByName('Sunset').AsDateTime;
            Close
          end   (* qSunriseSunset *);

        lbxAction.Clear;

        DecodeDateTime(dtpBetterSunsetTime.Time, Year, Month, Day, Hour, Minute, Second, Millisecond);
        DeleteStart := RecodeTime(BadSunset, Hour, Minute, Second, Millisecond);

        DecodeDateTime(dtpBetterSunrise.Time, Year, Month, Day, Hour, Minute, Second, Millisecond);
        DeleteStop  := RecodeTime(BadSunrise, Hour, Minute, Second, Millisecond);
        AddStep(Format('delete outside light from %s to %s',
                       [FormatDateTime('mm/dd hhnn:ss', DeleteStart),
                        FormatDateTime('mm/dd hhnn:ss', DeleteStop)
                       ]));

        AddStep(Format('make entry: zero outside light at %s',
                       [FormatDateTime('mm/dd hhnn:ss', DeleteStart)] ));
        LastSecond := RecodeTime(DeleteStart, 23, 59, 59, 0);
        AddStep(Format('make entry: zero outside light at %s',
                       [FormatDateTime('mm/dd hhnn:ss', LastSecond)] ));
        FirstSecond := RecodeTime(DeleteStop, 0, 0, 0, 0);
        AddStep(Format('make entry: zero outside light at %s',
                       [FormatDateTime('mm/dd hhnn:ss', FirstSecond)] ));
        AddStep(Format('make entry: zero outside light at %s',
                       [FormatDateTime('mm/dd hhnn:ss', DeleteStop )] ))
      end   (* tfmFixPhotocell.dtpBetterMorningChange *);

    procedure tfmFixPhotocell.FormDblClick(Sender: tObject);
      (* show master form *)
    
      begin (* tfmFixPhotocell.FormDblClick *)
        fmHC4Main.Show
      end   (* tfmFixPhotocell.FormDblClick *);

    procedure tfmFixPhotocell.FormShow(Sender: tObject);
      (* initialize *)
    
      begin (* tfmFixPhotocell.FormShow *)
        self.Color := UsualFormColor;
        dtpMorningAfter.Date := Today;
        dtpBetterMorning.Date := IncDay(Today, -1);
        dtpBetterMorningChange(nil)
      end   (* tfmFixPhotocell.FormShow *);

end.
