unit WeatherSetup;

interface

uses
	Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
		Dialogs, StdCtrls, EasyReg;

type
	TfrmWeatherSetup = class(TForm)
		rbZip: TRadioButton;
		rbCity: TRadioButton;
		txtZip: TEdit;
		txtCity: TEdit;
		btnGetLocID: TButton;
		gbLocID: TGroupBox;
		Label1: TLabel;
		Label2: TLabel;
		lblLocID: TLabel;
		gbUnits: TGroupBox;
		rbStandard: TRadioButton;
		rbMetric: TRadioButton;
		txtForecast: TEdit;
		Label3: TLabel;
		btnSave: TButton;
		Label4: TLabel;
		txtUpdateInt: TEdit;
		Label5: TLabel;
		txtForecastInt: TEdit;
		Label6: TLabel;
		Label7: TLabel;
		Label8: TLabel;
		EasyReg1: TEasyReg;
		Label9: TLabel;
		procedure btnSaveClick(Sender: TObject);
		procedure btnGetLocIDClick(Sender: TObject);
		procedure FormCreate(Sender: TObject);
		procedure txtCityChange(Sender: TObject);
		procedure txtZipChange(Sender: TObject);
		procedure txtCityEnter(Sender: TObject);
		procedure txtZipEnter(Sender: TObject);
	private
		Units: String;
		LocID: String;
		Forecast: Integer;
		UpdateInt: Integer;
		ForecastInt: Integer;
		procedure RegSave;
		procedure RegLoad;
		Function IsNumeric(s: String) : Boolean;
	public
		function Execute: Boolean;
	end;

var
	frmWeatherSetup: TfrmWeatherSetup;

implementation   

{$R *.dfm}

uses
	Weather, SelectCity;

function TfrmWeatherSetup.Execute: Boolean;
begin
	Result := (ShowModal = mrOk);
end;

procedure TfrmWeatherSetup.btnSaveClick(Sender: TObject);
begin
	if LocID = '' then
	begin
		ShowMessage('Location ID Not Set');
		exit;
	end;

	if rbStandard.Checked then
		Units := 's'
	else
		Units := 'm';

	try
		if StrToInt(txtUpdateInt.Text) > 30 then
			UpdateInt := StrToInt(txtUpdateInt.Text)
		else
			UpdateInt := 30;
	except
		UpdateInt := 30;
	end;

	Try
		if (StrToInt(txtForecast.Text) < 0) then
			Forecast := 0
		else if StrToInt(txtForecast.Text) > 10 then
			Forecast := 10
		else
			Forecast := StrToInt(txtForecast.Text);
	except
		Forecast := 0;
	end;

	try
		if StrToInt(txtForecastInt.text) > 2 then
			ForecastInt := StrToInt(txtForecastInt.text)
		else
			ForecastInt := 2;
	except
		ForecastInt := 2;
	end;

	RegSave;
	self.Close;
end;

procedure TfrmWeatherSetup.btnGetLocIDClick(Sender: TObject);
var
	i: Integer;
	WL: TWeatherLoc;
	SC: TfrmSelectCity;
begin
	if rbZip.Checked then
	begin
		if (Length(txtZip.Text) = 5) and IsNumeric(txtZip.Text) then
			LocID := txtZip.Text
		else
			ShowMessage('Not A Valid ZipCode');
	end
		else
	begin
		if txtCity.Text = '' then
			exit;
		WL := TWeatherLoc.Create(txtCity.Text, nil);
		try
			WL.DownloadAndResolve;
			if WL.Count = 1 then
			begin
				LocID := WL[0].ID;
				txtCity.Text := WL[0].City;
			end
				else if WL.Count > 1 then
			begin
				SC := TfrmSelectCity.Create(nil);
				try
					for i := 0 to WL.Count -1 do
						SC.ListBox1.Items.Add(WL[i].City);
					SC.Execute;
					LocID := WL[SC.SelectedIndex].ID;
					txtCity.Text := WL[SC.SelectedIndex].City;
				finally
					FreeAndNil(SC);
				end;
			end
				else
					ShowMessage('Error Retrieving LocID');
		finally
			FreeAndNil(WL);
		end;
	end;

	if LocID <> '' then
		lblLocID.Caption := LocID;
end;

procedure TfrmWeatherSetup.RegSave;
begin
	EasyReg1.Write('LocID', LocID);
	EasyReg1.Write('Units', Units);
	EasyReg1.Write('UpdateInt', UpdateInt);
	EasyReg1.Write('Forecast', Forecast);
	EasyReg1.Write('ForecastInt', ForecastInt);
end;

Procedure TfrmWeatherSetup.RegLoad;
begin
	if EasyReg1.Read('LocID', LocID) then
		lblLocID.Caption := LocID;

	if EasyReg1.Read('Units', Units) then
	begin
		if Units = 'm' then
			rbMetric.Checked := True;
	end;

	if EasyReg1.Read('UpdateInt', UpdateInt) then
		txtUpdateInt.Text := IntToStr(UpdateInt);

	if EasyReg1.Read('Forecast', Forecast) then
		txtForecast.Text := IntToStr(Forecast);

	if EasyReg1.Read('ForecastInt', ForecastInt) then
		txtForecastInt.Text := IntToStr(ForecastInt);
end;

Function TfrmWeatherSetup.IsNumeric(s: String) : Boolean;
var
	Code: Integer;
	Value: Double;
begin
	val(s, Value, Code);
	Result := (Code = 0);
end;

procedure TfrmWeatherSetup.FormCreate(Sender: TObject);
begin
	RegLoad;
end;

procedure TfrmWeatherSetup.txtCityChange(Sender: TObject);
begin
	btnGetLocID.Enabled := True;
end;

procedure TfrmWeatherSetup.txtZipChange(Sender: TObject);
begin
	btnGetLocID.Enabled := True;
end;

procedure TfrmWeatherSetup.txtCityEnter(Sender: TObject);
begin
	rbCity.Checked := True;
end;

procedure TfrmWeatherSetup.txtZipEnter(Sender: TObject);
begin
	rbZip.Checked := True;
end;

end.
