unit Utils;

interface

uses
  System.Classes, SysUtils, JS, Web, WEBLib.Forms, WEBLib.Toast, DateUtils;

procedure ShowStatusMessage(const AMessage, AClass: string; const AElementId: string);
procedure HideStatusMessage(const AElementId: string);
procedure ShowSpinner(SpinnerID: string);
procedure HideSpinner(SpinnerID: string);
function CalculateAge(DateOfBirth: TDateTime): Integer;
function FormatPhoneNumber(PhoneNumber: string): string;
procedure ApplyReportTitle(CurrentReportType: string);
// function FormatDollarValue(ValueStr: string): string;


implementation

procedure ShowStatusMessage(const AMessage, AClass: string; const AElementId: string);
var
  StatusMessage: TJSHTMLElement;
begin
  StatusMessage := TJSHTMLElement(document.getElementById(AElementId));
  if Assigned(StatusMessage) then
  begin
    if AMessage = '' then
    begin
      StatusMessage.style.setProperty('display', 'none');
      StatusMessage.className := '';
      StatusMessage.innerHTML := '';
    end
    else
    begin
      StatusMessage.innerHTML := AMessage;
      StatusMessage.className := 'alert ' + AClass;
      StatusMessage.style.setProperty('display', 'block');
    end
  end
  else
    console.log('Error: Status message element not found');
end;


procedure HideStatusMessage(const AElementId: string);
var
  StatusMessage: TJSHTMLElement;
begin
  StatusMessage := TJSHTMLElement(document.getElementById(AElementId));
  if Assigned(StatusMessage) then
  begin
    StatusMessage.style.setProperty('display', 'none');
    StatusMessage.className := '';
    StatusMessage.innerHTML := '';
  end
  else
    console.log('Error: Status message element not found');
end;


procedure ShowSpinner(SpinnerID: string);
var
  SpinnerElement: TJSHTMLElement;
begin
  SpinnerElement := TJSHTMLElement(document.getElementById(SpinnerID));
  if Assigned(SpinnerElement) then
  begin
    SpinnerElement.classList.remove('d-none');
    SpinnerElement.classList.add('d-block');
  end;
end;

procedure HideSpinner(SpinnerID: string);
var
  SpinnerElement: TJSHTMLElement;
begin
  SpinnerElement := TJSHTMLElement(document.getElementById(SpinnerID));
  if Assigned(SpinnerElement) then
  begin
    SpinnerElement.classList.remove('d-block');
    SpinnerElement.classList.add('d-none');
  end;
end;


function CalculateAge(DateOfBirth: TDateTime): Integer;
var
  Today, BirthDate: TJSDate;
  Year, Month, Day, BirthYear, BirthMonth, BirthDay: NativeInt;
  DOBString: string;
begin
  Today := TJSDate.New;
  Year := Today.FullYear;
  Month := Today.Month + 1;
  Day := Today.Date;
  // Formats the DateOfBirth as an ISO 8601 date string
  DOBString := FormatDateTime('yyyy-mm-dd', DateOfBirth);
  BirthDate := TJSDate.New(DOBString);
  if BirthDate = nil then
  begin
    Exit(0); // Exit the function with an age of 0 if the date creation fails
  end;

  BirthYear := BirthDate.FullYear;
  BirthMonth := BirthDate.Month + 1;
  BirthDay := BirthDate.Date;

  Result := Year - BirthYear;
  if (Month < BirthMonth) or ((Month = BirthMonth) and (Day < BirthDay)) then
    Dec(Result);
end;


function FormatPhoneNumber(PhoneNumber: string): string;
var
  Digits: string;
begin
  Digits := PhoneNumber.Replace('(', '').Replace(')', '').Replace('-', '').Replace(' ', '');
  case Length(Digits) of
    7:  Result := Format('%s-%s', [Copy(Digits, 1, 3), Copy(Digits, 4, 4)]);
    10: Result := Format('(%s) %s-%s', [Copy(Digits, 1, 3), Copy(Digits, 4, 3), Copy(Digits, 7, 4)]);
  else
    // If the number does not have 7 or 10 digits, whatever they typed is returned
    Result := PhoneNumber;
  end;
end;


procedure ApplyReportTitle(CurrentReportType: string);
var
  CrimeTitleElement: TJSHTMLElement;
begin
  CrimeTitleElement := TJSHTMLElement(document.getElementById('crime_title'));
  if Assigned(CrimeTitleElement) then
    CrimeTitleElement.innerText := CurrentReportType
  else
    Console.Log('Element with ID "crime_title" not found.');
end;


// Used html number input type to restrict the input instead of this function

// function FormatDollarValue(ValueStr: string): string;
// var
//   i: Integer;
// begin
//   Result := '';  // Initialize the result

//   // Filter out any characters that are not digits or decimal point
//   for i := 1 to Length(ValueStr) do
//   begin
//     if (Pos(ValueStr[i], '0123456789.') > 0) then
//     begin
//       Result := Result + ValueStr[i];
//     end;
//   end;
// end;

end.
