{*************************************************************************}
{                                                                         }
{ FilesUnit.pas - window to save games or load saved ones                 }
{                                                                         }
{ This file is a part of the project:                                     }
{   Dizzy Quest Player - player for *.dqe files, created in DQE2          }
{                                                                         }
{ © Parfenov Alexey Andreevich, 2007                                      }
{                                                                         }
{ e-mail: AlkatrazStudio@yandex.ru                                        }
{                                                                         }
{ This program is free software; you can redistribute it and/or modify    }
{ it under the terms of the GNU General Public License as published by    }
{ the Free Software Foundation; either version 2 of the License, or       }
{ (at your option) any later version.                                     }
{                                                                         }
{ This program is distributed in the hope that it will be useful,         }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of          }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            }
{ GNU General Public License for more details.                            }
{                                                                         }
{ You may read GNU General Public License at:                             }
{   http://www.gnu.org/copyleft/gpl.html                                  }
{                                                                         }
{*************************************************************************}
unit FilesUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,dqe2_types, StdCtrls, Buttons,alkatraz, ComCtrls;

type
  TForm_Files = class(TForm)
    Edit1: TEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    ListView1: TListView;
    procedure BitBtn2Click(Sender: TObject);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    function DoShow(DoSave:boolean):boolean;
    procedure BitBtn1Click(Sender: TObject);
    function SaveIt(SaveName:string;OverwritePrompt:boolean):boolean;
    procedure Localize;
    procedure FormCreate(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
  private
    { Private declarations }
    ok:boolean;
  public
    { Public declarations }
    lng:array of string;
  end;

var
  Form_Files: TForm_Files;
  save:boolean;

implementation

uses MainUnit;

{$R *.dfm}

procedure TForm_Files.BitBtn1Click(Sender: TObject);
var
  fn:string;
begin
  if edit1.Text='' then
  begin
    MessageDlgRus(lng[5],lng[6],MB_ICONWARNING or MB_OK);
    exit;
  end;
  if save then
  begin
    if SaveIt(Edit1.Text,true) then close;
  end
  else
  begin
    fn:=World.SharedDir+'\..\Saves\'+MainUnit.WorldName+'.'+Edit1.Text+'.dqe';
    if not FileExists(fn) then
    begin
      MessageDlgRus(lng[7],lng[8]+#13#10+Edit1.Text,MB_ICONQUESTION or MB_OK);
      exit;
    end;
    if not World.CanSave then
    begin
      MessageDlgRus(lng[15],lng[16],MB_OK);
      exit;
    end;
    world.LoadFromFile(fn,true);
    Form_Main_DQP.UpdatePlayer;
    ok:=true;
    close;
  end;
end;

procedure TForm_Files.BitBtn2Click(Sender: TObject);
begin
  close;
end;

function TForm_Files.DoShow(DoSave: boolean):boolean;
var
  f:TSearchRec;
  s:string;
  sys:_SYSTEMTIME;
  dt:TDateTime;
  i:TListItem;
begin
  ok:=false;
  save:=DoSave;
  if save then
  begin
    Form_Main_DQP.ImageList1.GetIcon(2,Self.Icon);
    Caption:=lng[0];
    BitBtn1.Caption:=lng[2];
  end
  else
  begin
    Form_Main_DQP.ImageList1.GetIcon(5,Self.Icon);
    Caption:=lng[1];
    BitBtn1.Caption:=lng[3];
  end;
  ListView1.Clear;
  if FindFirst(World.SharedDir+'\..\Saves\'+MainUnit.WorldName+'.*.dqe',faAnyFile,f)=0 then
  repeat
    if((f.Attr and fadirectory)=0)then
    begin
      s:=ExtractFileExt(ExtractFileNameOnly(f.name));
      s:=copy(s,2,length(s)-1);
      i:=ListView1.Items.Add;
      i.Caption:=s;
      FileTimeToSystemTime(f.FindData.ftLastWriteTime,sys);
      dt:=SystemTimeToDateTime(sys);
      i.SubItems.Add(FormatDateTime('ddddd tt',dt));
    end;
  until FindNext(f)<>0;
  FindClose(f);
  ShowModal;
  result:=ok;
end;

procedure TForm_Files.FormCreate(Sender: TObject);
begin
  SetLength(lng,17);
end;

procedure TForm_Files.ListView1Change(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  edit1.Text:=item.Caption;
end;

procedure TForm_Files.ListView1DblClick(Sender: TObject);
begin
  BitBtn1.Click;
end;

procedure TForm_Files.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,'_'+Name,lng);
  BitBtn2.Caption:=lng[4];
  ListView1.Columns[0].Caption:=lng[13];
  ListView1.Columns[1].Caption:=lng[14];
end;

function TForm_Files.SaveIt(SaveName: string;OverwritePrompt:boolean):boolean;
var
  fn:string;
begin
  result:=false;
  if world.CanSave then
  begin
    fn:=World.SharedDir+'\..\Saves\'+MainUnit.WorldName+'.'+SaveName+'.dqe';
    if OverwritePrompt then
      if FileExists(fn) then
        if MessageDlgRus(lng[9],lng[10]+#13#10+SaveName,MB_ICONQUESTION or MB_YESNO)<>mrYes then exit;
    world.SaveToFile(fn);
    result:=true;
    ok:=true;
  end
  else
  begin
    MessageDlgRus(lng[11],lng[12],MB_OK or MB_ICONINFORMATION);
  end;
end;

end.
