unit main;

{$mode objfpc}{$H+}
{$R music.res}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  ComCtrls, ExtendedTabControls, fphttpclient, opensslsockets, fpjson,
  jsonparser, ThreadUnit, uos, uos_portaudio, uos_flat, uos_libxmp, Windows;

type

  { TForm1 }

  TForm1 = class(TForm)
    BtnSearch: TButton;
    ExtendedTabControl1: TExtendedTabControl;
    ExtendedTabToolbar1: TExtendedTabToolbar;
    Label1: TLabel;
    Label2: TLabel;
    memoTest: TMemo;
    TxtBoxSearch: TEdit;
    lblSearch: TLabel;
    TabControl1: TTabControl;
    procedure BtnSearchClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure TxtBoxSearchChange(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  Client: TFPHTTPClient;
  Response: string;
  PackageName: string;
  jData: TJSONData;
  packageData: TJSONObject;
  test1: ThreadClass;
  ResStream: TResourceStream;

implementation

{$R *.lfm}

{ TForm1 }
function ExtractResourceToTempFile(const ResName: string): string;
var
  ResStream: TResourceStream;
  TempPath, TempFileName: string;
begin
  TempPath := GetTempDir;
  TempFileName := TempPath + '5f64126c-1f61-4e65-8250-4a7835d2f155';

  ResStream := TResourceStream.Create(HInstance, ResName, RT_RCDATA);
  try
    ResStream.SaveToFile(TempFileName);
  finally
    ResStream.Free;
  end;

  Result := TempFileName;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  BtnSearch.Enabled := False;
   //loonie_mifue.xm

   uos_LoadLib( pchar('LibPortaudio-64.dll'), nil, nil, nil, nil, nil, pchar('libxmp-64.dll'));
   uos_CreatePlayer( 0 );
   uos_AddIntoDevOut( 0 );
   uos_AddFromFile( 0, PChar(ExtractResourceToTempFile('LOONIEMUSIC')));
   uos_Play(0);
end;

procedure TForm1.BtnSearchClick(Sender: TObject);
begin
  PackageName := TxtBoxSearch.Text;
  Client := TFPHTTPClient.Create(nil);
  try
    test1 := ThreadClass.create();
    Response := Client.Get(Concat('https://registry.npmjs.org/', PackageName));
    jData := GetJSON(Response);
    packageData := jData as TJSONObject;

    memoTest.Lines.Text := jData.FormatJSON;
  finally
    Client.Free;
  end;
end;

procedure TForm1.TabControl1Change(Sender: TObject);
begin

end;

procedure TForm1.TxtBoxSearchChange(Sender: TObject);
begin
  if Length(TxtBoxSearch.Text) > 1 then
    BtnSearch.Enabled := True
  else
    BtnSearch.Enabled := False;
end;
end.
uos_free;
