{
********************************************************************************

YaPeTaVi - Yet another Periodic Table Viewer
Copyright (C) 2009-2010 Geoffray Levasseur <geoffray.levasseurbrandin@numericable.fr>.
All rights reserved.
http://www.geoffray-levasseur.org/

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 3 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 should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA  02111-1307, USA.

********************************************************************************

Description:
  Element array initialization and test unit

}
unit uInit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils; 

procedure InitTable;
function TestTable: Boolean;
procedure InitPaths;
procedure InitIcons;

implementation

uses
  uElemListClass, uUtils, uConst, uStrings, uIconManager, Controls, Forms,
  uDebug, uCommon;

procedure InitTable;
{$warning Is there a way to do this more efficiently?}
begin //add each element in order
  ElementsList := TElemList.Create(nil);
  ElementsList.BeginUpdate;
  ElementsList.Add(H);
  ElementsList.Add(He);
  ElementsList.Add(Li);
  ElementsList.Add(Be);
  ElementsList.Add(B);
  ElementsList.Add(C);
  ElementsList.Add(N);
  ElementsList.Add(O);
  ElementsList.Add(F);
  ElementsList.Add(Ne);
  ElementsList.Add(Na);
  ElementsList.Add(Mg);
  ElementsList.Add(Al);
  ElementsList.Add(Si);
  ElementsList.Add(P);
  ElementsList.Add(S);
  ElementsList.Add(Cl);
  ElementsList.Add(Ar);
  ElementsList.Add(K);
  ElementsList.Add(Ca);
  ElementsList.Add(Sc);
  ElementsList.Add(Ti);
  ElementsList.Add(V);
  ElementsList.Add(Cr);
  ElementsList.Add(Mn);
  ElementsList.Add(Fe);
  ElementsList.Add(Co);
  ElementsList.Add(Ni);
  ElementsList.Add(Cu);
  ElementsList.Add(Zn);
  ElementsList.Add(Ga);
  ElementsList.Add(Ge);
  ElementsList.Add(Ars);
  ElementsList.Add(Se);
  ElementsList.Add(Br);
  ElementsList.Add(Kr);
  ElementsList.Add(Rb);
  ElementsList.Add(Sr);
  ElementsList.Add(Y);
  ElementsList.Add(Zr);
  ElementsList.Add(Nb);
  ElementsList.Add(Mo);
  ElementsList.Add(Tc);
  ElementsList.Add(Ru);
  ElementsList.Add(Rh);
  ElementsList.Add(Pd);
  ElementsList.Add(Ag);
  ElementsList.Add(Cd);
  ElementsList.Add(Ind);
  ElementsList.Add(Sn);
  ElementsList.Add(Sb);
  ElementsList.Add(Te);
  ElementsList.Add(I);
  ElementsList.Add(Xe);
  ElementsList.Add(Cs);
  ElementsList.Add(Ba);
  ElementsList.Add(La);
  ElementsList.Add(Ce);
  ElementsList.Add(Pr);
  ElementsList.Add(Nd);
  ElementsList.Add(Pm);
  ElementsList.Add(Sm);
  ElementsList.Add(Eu);
  ElementsList.Add(Gd);
  ElementsList.Add(Tb);
  ElementsList.Add(Dy);
  ElementsList.Add(Ho);
  ElementsList.Add(Er);
  ElementsList.Add(Tm);
  ElementsList.Add(Yb);
  ElementsList.Add(Lu);
  ElementsList.Add(Hf);
  ElementsList.Add(Ta);
  ElementsList.Add(W);
  ElementsList.Add(Re);
  ElementsList.Add(Os);
  ElementsList.Add(Ir);
  ElementsList.Add(Pt);
  ElementsList.Add(Au);
  ElementsList.Add(Hg);
  ElementsList.Add(Tl);
  ElementsList.Add(Pb);
  ElementsList.Add(Bi);
  ElementsList.Add(Po);
  ElementsList.Add(At);
  ElementsList.Add(Rn);
  ElementsList.Add(Fr);
  ElementsList.Add(Ra);
  ElementsList.Add(Ac);
  ElementsList.Add(Th);
  ElementsList.Add(Pa);
  ElementsList.Add(U);
  ElementsList.Add(Np);
  ElementsList.Add(Pu);
  ElementsList.Add(Am);
  ElementsList.Add(Cm);
  ElementsList.Add(Bk);
  ElementsList.Add(Cf);
  ElementsList.Add(Es);
  ElementsList.Add(Fm);
  ElementsList.Add(Md);
  ElementsList.Add(No);
  ElementsList.Add(Lr);
  ElementsList.Add(Rf);
  ElementsList.Add(Db);
  ElementsList.Add(Sg);
  ElementsList.Add(Bh);
  ElementsList.Add(Hs);
  ElementsList.Add(Mt);
  ElementsList.Add(Ds);
  ElementsList.Add(Rg);
  ElementsList.Add(Cn);
  ElementsList.EndUpdate;
end;

function TestTable: Boolean;
var
  N: integer;
begin
  Result := True;
  for N := 1 to ElemNumber do
    if ElementsList.Elements[N].Number <> N then
    begin
      Result := False;
      PrintLnDbg('*** ERROR: uInit.TestTable: Invalid order in the list: Get ' +
        IntToStr(N) + ' but should be ' + IntToStr(ElementsList.Elements[N].Number) +
        '. Please contact application developper!', vlLow);
      end;
  if Result then
    PrintLnDbg('Table seems to be Ok.', vlHigh)
  else
  begin
    PrintLnDbg('*** uInit.TestTable: The table will probably contains errors!',
      vlLow);
    PrintLnDbg('*** uInit.TestTable: Some program functionalities may be ' +
      'affected...', vlLow);
  end;
end;

procedure InitPaths;
var
  Err: LongInt;
begin
  sPrgmPath := GetProgramPath;
  Err := FormatFileName(GetUserDir + '.yapetavi', sUsrPath);
  if sUsrPath[Length(sUsrPath)] <> DirectorySeparator then
    sUsrPath := sUsrPath + DirectorySeparator;
  if Err = 0 then
  begin
    MkDir(sUsrPath);
    PrintLnDbg('User path is: ' + sUsrPath + ' created successfully...', vlLow);
  end else
    if Err = $1000 then
      PrintLnDbg('User path is: ' + sUsrPath, vlLow)
    else
      if Err = $2000 then
      begin
        PrintLnDbg('*** ERROR: uInit.InitPaths: .yapetavi is a file, can''t ' +
          'create this directory', vlLow);
        PrintLnDbg(erSolveFileConflict, vlLow);
        Halt(10);
      end;
  Err := FormatFileName(sUsrPath + 'db', sDbPath);
  if Err = 0 then
  begin
    MkDir(sDbPath);
    PrintLnDbg('Database path is: ' + sDbPath + ' created successfully...', vlLow);
  end else
    if Err = $1000 then
      PrintLnDbg('Database path is: ' + sDbPath, vlLow)
    else
      if Err = $2000 then
      begin
        PrintLnDbg('*** ERROR: uInit.InitPaths: .yapetavi/db is a file, can''t ' +
          'create this directory', vlLow);
        PrintLnDbg(erSolveFileConflict, vlLow);
        Halt(11);
      end;
  Err := FormatFileName(sUsrPath + 'pics', sPicPath);
  if Err = 0 then
   begin
     MkDir(sPicPath);
     PrintLnDbg('Pictures path is: ' + sPicPath + ' created successfully...', vlLow);
   end else
     if Err = $1000 then
       PrintLnDbg('   Pictures path is: ' + sPicPath, vlLow)
     else
       if Err = $2000 then
       begin
         PrintLnDbg('*** ERROR: uInit.InitPaths: .yapetavi/pic is a file, can''t ' +
           'create this directory', vlLow);
        PrintLnDbg(erSolveFileConflict, vlLow);
         Halt(13);
       end;
  Err := FormatFileName(GetProgramPrefix, sPrefixPath); //the directory must exists so no more checks
end;

procedure InitIcons;
var
  Err: LongInt;
begin
  Err := FormatFileName(GetIconSetsBaseDir, sIconsBase);
  GetIconSets(sIconsBase, IconSetsList);
  Err := FormatFileName(FindOxygen, sIconPath);
  ilDefault := TImageList.Create(Application);
  ilDefault.Masked := False;
end;

end.

