unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  System.Permissions,
  {$IFDEF ANDROID}
  FMX.Platform.Android,
  Androidapi.JNI.Net, FMX.Platform,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNIBridge,
  Androidapi.Helpers,
  Androidapi.JNI.Telephony ,FMX.PhoneDialer, Androidapi.JNI.Os,
  {$ENDIF}
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.ListView.Types,
  FMX.ListView.Appearances, FMX.ListView.Adapters.Base, FMX.ListView,
  FMX.ScrollBox, FMX.Memo,FMX.DialogService, FMX.Layouts, FMX.ListBox,
  System.ImageList, FMX.ImgList, FMX.Ani, FMX.Objects, FMX.Effects,
  FMX.Memo.Types, FMX.Edit, FMX.Media;

type
  TForm5 = class(TForm)
    StyleBook1: TStyleBook;
    Panel1: TPanel;
    opd_names: TOpenDialog;
    mem_names: TMemo;
    lsb_names: TListBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    ImageList1: TImageList;
    spb_loadname: TSpeedButton;
    spb_go: TSpeedButton;
    lbl_winner: TLabel;
    Timer1: TTimer;
    FloatAnimation1: TFloatAnimation;
    FloatAnimation2: TFloatAnimation;
    mem_winners: TMemo;
    Image1: TImage;
    GlowEffect1: TGlowEffect;
    FloatAnimation3: TFloatAnimation;
    ShadowEffect1: TShadowEffect;
    Edit1: TEdit;
    spb_loadmusic: TSpeedButton;
    MediaPlayer1: TMediaPlayer;
    opd_music: TOpenDialog;
    procedure spb_loadnameClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure spb_goClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure spb_loadmusicClick(Sender: TObject);
  private
    tmpi:Integer;
    musicName:String;
  public
    procedure LoadList();
  end;

var
  Form5: TForm5;

implementation

{$R *.fmx}

procedure TForm5.LoadList();
var
  i:Integer;
begin
  if FileExists(edit1.Text.Trim) then
  begin
    FloatAnimation3.Enabled:=false;
    lbl_winner.Visible:=false;
    mem_names.Lines.Clear;
    mem_names.Lines.LoadFromFile(edit1.Text.Trim,TEncoding.UTF8);
    mem_names.Text:= mem_names.Text.Trim;
    lsb_names.Items.Clear;
    mem_winners.Lines.Clear;
    GlowEffect1.Enabled:=false;
    ShadowEffect1.Enabled:=false;
    for i := 0 to mem_names.Lines.Count -1 do
    begin
      lsb_names.Items.Add(mem_names.Lines[i]);
      lsb_names.ItemByIndex(i).ImageIndex:=0;
    end;
    TDialogService.MessageDialog('加载列表总共：'+IntToStr(lsb_names.Items.Count)+'个项目。' ,TMsgDlgType.mtInformation,[TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, -1,nil);
  end
  else
  begin
    TDialogService.MessageDialog('加载文件取消！' ,TMsgDlgType.mtError,[TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, -1,nil);
  end;
end;

procedure TForm5.FormCreate(Sender: TObject);
var
  FPermissionReadExternalStorage,
  FPermissionWriteExternalStorage:String;
begin
  {$IFDEF ANDROID}
    //FPermissionCamera := JStringToString(TJManifest_permission.JavaClass.CAMERA);
    FPermissionReadExternalStorage := JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE);
    FPermissionWriteExternalStorage := JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE);

    PermissionsService.RequestPermissions([FPermissionReadExternalStorage,FPermissionWriteExternalStorage],
      procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
      begin
        if (AGrantResults[0] = TPermissionStatus.Granted) then
        begin
          TDialogService.ShowMessage('STORAGE permission granted, OK.');
        end
        else
        begin
          TDialogService.ShowMessage('STORAGE permission not granted');
        end;
      end);
  {$ENDIF}

  mem_names.Lines.Clear;
  mem_winners.Lines.Clear;
  lsb_names.Items.Clear;
  lbl_winner.Visible:=false;
  GlowEffect1.Enabled:=false;
  ShadowEffect1.Enabled:=false;
  FloatAnimation3.Enabled:=false;
  musicName:='/storage/emulated/0/背景音乐.mp3';
end;

procedure TForm5.spb_goClick(Sender: TObject);
begin
  if spb_go.Text='开始摇奖' then
    begin
    if lsb_names.Items.Count<=0 then
    begin
      TDialogService.MessageDialog('请先加载抽奖人员名单！' ,TMsgDlgType.mtError,[TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, -1,nil);
    end
    else if lsb_names.Items.Count<=1 then
    begin
      TDialogService.MessageDialog('仅有一个人无法抽奖哦！' ,TMsgDlgType.mtError,[TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, -1,nil);
    end
    else
    begin
      if mem_winners.Lines.Count>=lsb_names.Items.Count then
      begin
        TDialogService.MessageDialog('所有人都中奖了！' ,TMsgDlgType.mtWarning,[TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, -1,nil);
      end
      else
      begin
        lbl_winner.Visible:=true;
        spb_go.Text:='停止摇奖';
        spb_go.Hint:='停止摇奖';
        spb_loadname.Enabled:=false;
        spb_loadmusic.Enabled:=false;
        spb_go.StyleLookup:='pausetoolbuttonbordered';
        GlowEffect1.Enabled:=false;
        ShadowEffect1.Enabled:=true;
        FloatAnimation1.Enabled:=true;
        FloatAnimation2.Enabled:=true;
        FloatAnimation3.Enabled:=false;
        GlowEffect1.Softness:=0.4;
        lbl_winner.TextSettings.Font.Size:=112;
        Timer1.Enabled:=true;
        if FileExists(musicName) then
        begin
          MediaPlayer1.FileName:=musicName;
          MediaPlayer1.Play;
        end;
      end;
    end;
  end
  else
  begin
    MediaPlayer1.Stop;
    spb_loadname.Enabled:=true;
    spb_loadmusic.Enabled:=true;
    spb_go.Text:='开始摇奖';
    spb_go.Hint:='开始摇奖';
    spb_go.StyleLookup:='playtoolbuttonbordered';
    FloatAnimation1.Enabled:=false;
    FloatAnimation2.Enabled:=false;
    lbl_winner.RotationAngle:=0;
    Timer1.Enabled:=false;
    Randomize;
    repeat
      tmpi:=Random(lsb_names.Items.Count);
    until mem_winners.Lines.IndexOf(lsb_names.Items[tmpi])<0;
    lsb_names.ItemByIndex(tmpi).IsSelected:=true;
    lbl_winner.Text := lsb_names.Items[tmpi];
    mem_winners.Lines.Add(lbl_winner.Text);
    lbl_winner.TextSettings.Font.Size:=168;
    GlowEffect1.Enabled:=true;
    ShadowEffect1.Enabled:=false;
    FloatAnimation3.Enabled:=true;
  end;
end;

procedure TForm5.spb_loadmusicClick(Sender: TObject);
begin
  if opd_music.Execute then
  begin
     musicName:=opd_music.FileName;
  end
end;

procedure TForm5.spb_loadnameClick(Sender: TObject);
begin
  if opd_names.Execute then
  begin
     edit1.Text:=opd_names.FileName;
  end;
  LoadList();
end;

procedure TForm5.Timer1Timer(Sender: TObject);
begin
    Randomize;
    tmpi:=Random(lsb_names.Items.Count);
    lsb_names.ItemByIndex(tmpi).IsSelected:=true;
    lbl_winner.Text := lsb_names.Items[tmpi];
    Application.ProcessMessages;
end;

end.
