unit WeatherThisDate;

  interface
    uses
      AdsData       (* used by AdsTable *),
      AdsFunc       (* used by AdsTable *),
      AdsTable      (* tAdsQuery *),
      AdvGrid       (* glCustom *),
      AdvObj        (* TMS *),
      AdvUtil       (* TMS *),
      BaseGrid      (* TMS *),
      Buttons       (* tBitBtn *),
      Classes,
      ComCtrls      (* tDateTimePicker *),
      Controls      (**),
      DB            (* tDataSet, tDataSource, tDateField, tFloatField,
                         tIntegerField, tStringField; also used by AdsTable *),
      DBAdvGrid     (* tDBAdvGrid *),
      Forms         (* tForm *),
      Graphics      (* TMS *),
      Grids         (* TMS *),
      ppBands       (* tppDetailBand, tppFooterBand, tppGroupFooterBand,
                         tppGroupHeaderBand, tppHeaderBand, tppSummaryBand,
                         tppTitleBand *),
      ppCache,  
      ppClass       (* tppGroup *),
      ppComm,
      ppCtrls       (* tppDBCalc, tppDBText, tppLabel, tppLine, tppShape *),
      ppDB,
      ppDBPipe      (* tppDBPipeline *),
      ppDesignLayer (* tppDesignLayer, tppDesignLayers *),
      ppMemo        (* tppMemo *),
      ppParameter   (* tppParameterList *),
      ppPrnabl,
      ppProd,
      ppRelatv,
      ppReport      (* tppChildReport, tppReport *),
      ppStrtch      (* for ppMemo *),
      ppSubRpt      (* tppSubReport *),
      ppVar         (* tppSystemVariable *),
      StdCtrls      (* tButton, tLabel *),
      Windows;

    type
      tfmWeatherThisDate = class(tForm)
          qWeatherThisDate: tAdsQuery;
            qWeatherThisDateTheDate: tDateField;
            qWeatherThisDateAvgTempF: tFloatField;
            qWeatherThisDateMaxTempF: tFloatField;
            qWeatherThisDateMinTempF: tFloatField;
            qWeatherThisDateRainmm:   tFloatField;
            qWeatherThisDateTheYear: tIntegerField;
            qWeatherThisDateXTempRange: tStringField;
            dataWeatherThisDate: tDataSource;
              dbadvgridWeatherThisDate: tDBAdvGrid;
              pipeWeather: tppDBPipeline;
                reportWeather: tppReport;
                  rbparamsWeather: tppParameterList;
                  ppDesignLayersWeather: tppDesignLayers;
                    ppDesignLayerWeather: tppDesignLayer;
                  rbbdWeatherHeader: tppHeaderBand;
                    rblbHeaderRain:  tppLabel;
                    rblbHeaderTempF: tppLabel;
                    rblbHeaderTitle: tppLabel;
                    rblbHeaderYear:  tppLabel;
                  rbbdWeatherDetail: tppDetailBand;
                    rbdbtxDetailRain:      tppDBText;
                    rbdbtxDetailTempRange: tppDBText;
                    rbdbtxDetailYear:      tppDBText;
                  rbbdWeatherFooter: tppFooterBand;
                    rbsysFooterDateTime: tppSystemVariable;
          btnPrintReport: tBitBtn;
          btnShowWeather: tButton;
          dtpPicker: tDateTimePicker;
          lbWeatherOnDate: tLabel;

          procedure qWeatherThisDateCalcFields(DataSet: tDataSet);

          procedure btnPrintReportClick(Sender: tObject);
          procedure btnShowWeatherClick(Sender: tObject);
          procedure dtpPickerClick     (Sender: tObject);
          procedure FormDblClick       (Sender: tObject);
          procedure FormShow           (Sender: tObject);
        end (* tfmWeatherThisDate *);

    var
      fmWeatherThisDate: tfmWeatherThisDate;

  implementation
    uses
      DateUtils (* DayOf, MonthOf, Today *),
      Dialogs   (* ShowMessage *),
      HC3DM     (* Global *),
      HC4Main   (* fmHC4Main *),
      MyConsts  (* UsualFormColor *),
      SysUtils  (* Format, FormatDateTime *);
      {$R *.dfm}

    procedure tfmWeatherThisDate.btnPrintReportClick(Sender: tObject);
      (* ReportBuilder does all the work *)
    
      begin (* tfmWeatherThisDate.btnPrintReportClick *)
        case Global.Status of
              gsConnected,
          gsReceivingDump: ShowMessage('Do not use when connected to base station.');

            gsUnconnected: begin (* OK to proceed *)
                             ShowMessage('Expect crash on closing preview.');
                             with rblbHeaderTitle do
                               Caption := FormatDateTime('"weather here on " mmmm d',
                                                         dtpPicker.Date);
                             reportWeather.PrintReport
                           end (* OK to proceed *)
          end (* case *)
      end   (* tfmWeatherThisDate.btnPrintReportClick *);

    procedure tfmWeatherThisDate.btnShowWeatherClick(Sender: tObject);
      (* run the query *)
    
      begin (* tfmWeatherThisDate.btnShowWeatherClick *)
        with qWeatherThisDate do
          begin (* qWeatherThisDate *)
            Close;
            ParamByName('Month').AsInteger := MonthOf(dtpPicker.Date);
            ParamByName('Day'  ).AsInteger := DayOf  (dtpPicker.Date);
            Open
          end   (* qWeatherThisDate *);
        lbWeatherOnDate.Caption := FormatDateTime('"weather on "mmmm d',
                                                 dtpPicker.Date);
        lbWeatherOnDate.Visible := true
      end   (* tfmWeatherThisDate.btnShowWeatherClick *);

    procedure tfmWeatherThisDate.dtpPickerClick(Sender: tObject);
      (* date chosen *)
    
      begin (* tfmWeatherThisDate.dtpPickerClick *)
        btnShowWeather.Caption := FormatDateTime('"show weather on "mmmm d',
                                                 dtpPicker.Date)
      end   (* tfmWeatherThisDate.dtpPickerClick *);

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

    procedure tfmWeatherThisDate.FormShow(Sender: tObject);
      (* initialize *)
    
      begin (* tfmWeatherThisDate.FormShow *)
        self.Color := UsualFormColor;
        dtpPicker.Date := Today;
        btnShowWeather.Caption := FormatDateTime('"show weather on "mmmm d', Today);
        lbWeatherOnDate.Visible := false
      end   (* tfmWeatherThisDate.FormShow *);

    procedure tfmWeatherThisDate.qWeatherThisDateCalcFields(DataSet: tDataSet);
      (* prettyprinting *)

      var
        MaxTempF, MeanTempF, MinTempF: real;

      begin (* tfmWeatherThisDate.qWeatherThisDateCalcFields *)
        with qWeatherThisDate do
          begin (* qWeatherThisDate *)
            MaxTempF  := FieldByName('MaxTempF').AsFloat;
            MeanTempF := FieldByName('AvgTempF').AsFloat;
            MinTempF  := FieldByName('MinTempF').AsFloat;

            with FieldByName('XTempRange') do
              AsString := Format('%.0f (%.0f - %.0f)',
                                 [MeanTempF, MinTempF, MaxTempF])
          end   (* qWeatherThisDate *)
      end   (* tfmWeatherThisDate.qWeatherThisDateCalcFields *);

end.
