#include <WindowsAPI.js>

function UnzipData(ziped_file_name)
{
  if(GetFileAttributes(ziped_file_name)<0)
  {
    alert(ziped_file_name+" is not exists.");
    return false;
  }
  bjShell=new ActiveXObject("Wscript.Shell");

  zip_ext="\""+System.GetAppPath()+"7-Zip\\7z.exe\"";
  zip_action=" e";
  zip_option_r=" -r -y";
  data_path=System.GetAppPath()+"Data";
  
  cmd_line=zip_ext+zip_action+" \""+ziped_file_name+"\" -o\""+data_path+"\""+zip_option_r;
  //editFileName.Text=cmd_line;

  bjShell.Run(cmd_line,0,true);
  return true;
}

function SelectFileButtonClick(Sender)
{
 if(LANG_CODE==2052)
   labelTip.Caption="请选择文件";
 else
   labelTip.Caption="Select file first";
 
 OpenDialog1=dvm.GetSharedObject(scope+".RestoreData.OpenDialog1");
 if(OpenDialog1.Execute())
 {
   editFileName.Text=OpenDialog1.FileName;
 }

 if(LANG_CODE==2052)
   labelTip.Caption="文件已选择";
 else
   labelTip.Caption="File selected";
}

function OnRestoreButtonClick(Sender)
{
 if(LANG_CODE==2052)
   labelTip.Caption="正在恢复";
 else
   labelTip.Caption="Retoring";

 ziped_file_name=editFileName.Text;
 if(UnzipData(ziped_file_name))
 {
   if(LANG_CODE==2052)
     labelTip.Caption="恢复完成";
   else
     labelTip.Caption="Restore finish";
 }else
 {
   if(LANG_CODE==2052)
     labelTip.Caption="恢复出错";
   else
     labelTip.Caption="Restore error";
 }
}

function ShowRestoreForm()
{
  vcl=new dobject("vcl");
  fn=System.GetAppPath()+"RestoreData.dfm";
  scope="RestoreDataApp";
  share_all=true;
  form=vcl.LoadFormFromDfmFile(fn,scope,share_all);
  form.OnClose=function(Sender,Action){Sender.operator_delete();};
  
  dvm.GetSharedObject(scope+".RestoreData.btnSelectFile").OnClick=SelectFileButtonClick;
  btnRestore=dvm.GetSharedObject(scope+".RestoreData.btnRestore");
  btnRestore.OnClick=OnRestoreButtonClick;

  editFileName=dvm.GetSharedObject(scope+".RestoreData.editFileName");
  labelTip=dvm.GetSharedObject(scope+".RestoreData.labelTip");
  
  if(LANG_CODE!=2052)
  {
    form.Caption="Duceland Widgets Engine data restore";
    dvm.GetSharedObject(scope+".RestoreData.labelFileName").Caption="Zip file:";
    btnRestore.Caption="Restore overwritten";
  }
  
  form.Show();
}

ShowRestoreForm();
