(*
 Additional types and functions that can be used in scripts:

  //Types
  TWIDEARRAY : array of String

  //Field functions
  procedure AddSearchResult(Title1, Title2, Year, URL, PreviewURL : String)
  procedure AddFieldValue(AField: Integer; AValue : String)
  procedure AddMoviePerson(Name, TransName, Role, URL : String; AType : Byte)
  procedure AddPersonMovie(Title, OrigTitle, Role, Year, URL : String; AType : Byte)
  procedure AddAward(Event, Award, Category, Recipient, Year: String; const Won : Boolean)
  procedure AddConnection(Title, OrigTitle, Category, URL, Year: String)
  procedure AddEpisode(Title, OrigTitle, Description, URL, Year, Season, Episode : String)
  
  //String functions
  function Pos(Substr : String; Str: String): Integer
  function PosFrom(const SubStr, Str : String; FromIndex : Integer) : Integer
  function LastPos(const SubStr, Str : String) : Integer
  function PrevPos(const SubStr, Str : String; APos : Integer) : Integer
  function RemoveTags(AText : String; doLineBreaks : Boolean) : String
  function ExplodeString(AText : String; var Items : TWideArray; Delimiters : String) : Integer
  function Copy(S: String; Index, Count: Integer): String
  procedure Delete(var S: String; Index, Count: Integer)
  procedure Insert(Source: String; var Dest: String; Index: Integer)
  function Length(S: String): Integer
  function Trim(S: String): String
  function CompareText(S1, S2: String): Integer
  function CompareStr(S1, S2: String): Integer
  function UpperCase(S: String): String
  function LowerCase(S: String): String
  function StringReplace(S, OldPattern, NewPattern: String; ReplaceAll : Boolean; IgnoreCase : Boolean; WholeWord: Boolean): String
  function StrToInt(const S: String): Integer
  function IntToStr(const Value: Integer): String
  function StrToFloat(const S: String): Extended
  function FloatToStr(const Value: Extended): String

  function HTMLValues(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String
  function HTMLValues2(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String
  function TextBetween(const HTML : String; ABegin, AEnd : String; doLineBreaks : Boolean; var Pos : Integer) : String
  function HTMLToText(const HTML : String) : String

  procedure ShowMessage(const Msg, Head : String)
*)

//Some useful constants
const
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch = 0; 
 smNormal = 1;
  
 //Parse results
 prError     = 0; 
 prFinished  = 1;
 prList      = 2;
 prListImage = 3;
 prDownload  = 4;
  
 //Movie fields
 mfURL         = 0;
 mfTitle       = 1;
 mfOrigTitle   = 2;
 mfAka         = 3;
 mfYear        = 4;
 mfGenre       = 5;
 mfCategory    = 6;
 mfCountry     = 7;
 mfStudio      = 8;
 mfMPAA        = 9;
 mfRating      = 10;
 mfTags        = 11;
 mfTagline     = 12;
 mfDescription = 13;
 mfDuration    = 14;
 mfFeatures    = 15;
  
 //People fields
 pfURL        = 0;
 pfName       = 1;
 pfTransName  = 2;
 pfAltNames   = 3;
 pfBirthday   = 4;
 pfBirthplace = 5;
 pfGenre      = 6;
 pfBio        = 7;
 pfDeathDate  = 8;
  
 //Credits types
 ctActors    = 0;
 ctDirectors = 1;
 ctWriters   = 2;
 ctComposers = 3;
 ctProducers = 4;
  
 //Script data 
 SCRIPT_VERSION = '0.1.0.1'; 
 SCRIPT_NAME    = 'Текстовый файл';
 SCRIPT_DESC    = '[RU] Получить информацию о фильме из текстового файла';
 SCRIPT_LANG    = $19; //Russian
 SCRIPT_TYPE    = stMovies;
 
 CODE_PAGE   = 0; //Use 0 for Autodetect 

//Global variables 
var
 Mode : Byte;

//Functions 
function GetScriptVersion : String;
begin
 Result := SCRIPT_VERSION;
end; 
 
function GetScriptName : String;
begin
 Result :=  SCRIPT_NAME;
end;

function GetScriptDesc : String;
begin
 Result := SCRIPT_DESC;
end;

function GetScriptLang: Cardinal;
begin
 Result := SCRIPT_LANG;
end;

function GetCodePage : Cardinal;
begin
 Result := CODE_PAGE;
end;

function GetBaseURL : AnsiString;
begin
 Result := '';
end;

function GetDownloadURL : AnsiString;
var
 Path : String;
 P : Integer;
begin
 Path := GetFieldValue(31);
 P := Pos('|', Path);
 if P < 1 then
  P := Length(Path) + 1;
 Path := Copy(Path, 1, P - 1); 
 if Path <> '' then
  Path := ChangeFileExt(Path, '.txt')
 else
  Path := 'C:\dummy.txt'; 
  
 Result := Path;
end;

function GetScriptType : Byte; 
begin
 Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte; 
begin
 Result := Mode;
end;
  
procedure ParseMovie(HTML : String);
var
 curPos, EndPos, P : Integer;
 V : TWideArray;
 I : Integer;
begin
 curPos := Pos('Название:', HTML);
 curPos := curPos + Length('Название:');
 EndPos := PosFrom(#13, HTML, curPos);
 
 AddFieldValue(mfTitle, Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 curPos := PosFrom('Оригинальное название:', HTML, EndPos);
 curPos := curPos + Length('Оригинальное название:');
 EndPos := PosFrom(#13, HTML, curPos);
 
 AddFieldValue(mfOrigTitle, Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 curPos := PosFrom('Производство:', HTML, EndPos);
 curPos := curPos + Length('Производство:');
 EndPos := PosFrom(#13, HTML, curPos);
 
 AddFieldValue(mfCountry, Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 curPos := PosFrom('Год выпуска:', HTML, EndPos);
 curPos := curPos + Length('Год выпуска:');
 EndPos := PosFrom(#13, HTML, curPos);
 
 AddFieldValue(mfYear, Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 curPos := PosFrom('Продолжительность:', HTML, EndPos);
 curPos := curPos + Length('Продолжительность:');
 EndPos := PosFrom(' min', HTML, curPos);
 
 AddFieldValue(mfDuration, Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 curPos := PosFrom('В ролях:', HTML, EndPos);
 curPos := curPos + Length('В ролях:');
 EndPos := PosFrom(#13, HTML, curPos);
 P := PosFrom(' и др.', HTML, curPos); 
 if (P > 0) AND (P < EndPos) then
  EndPos := P;
 ExplodeString(Trim(Copy(HTML, curPos, EndPos - curPos)), V, ',');
 for I := Low(V) to High(V) do
  AddMoviePerson(V[I], '', '', '', ctActors);
  
 curPos := PosFrom('Сценарий:', HTML, EndPos);
 curPos := curPos + Length('Сценарий:');
 EndPos := PosFrom(#13, HTML, curPos);
 P := PosFrom(' и др.', HTML, curPos); 
 if (P > 0) AND (P < EndPos) then
  EndPos := P;
 ExplodeString(Trim(Copy(HTML, curPos, EndPos - curPos)), V, ',');
 for I := Low(V) to High(V) do
  AddMoviePerson(V[I], '', '', '', ctWriters); 
  
 curPos := PosFrom('Режиссёр:', HTML, EndPos);
 curPos := curPos + Length('Режиссёр:');
 EndPos := PosFrom(#13, HTML, curPos);
 P := PosFrom(' и др.', HTML, curPos); 
 if (P > 0) AND (P < EndPos) then
  EndPos := P;
 ExplodeString(Trim(Copy(HTML, curPos, EndPos - curPos)), V, ',');
 for I := Low(V) to High(V) do
  AddMoviePerson(V[I], '', '', '', ctDirectors);  
 
 curPos := PosFrom('Описание:', HTML, EndPos);
 curPos := curPos + Length('Описание:');
 EndPos := PosFrom(#13#10#13#10, HTML, curPos);
 
 AddFieldValue(mfDescription, Trim(Copy(HTML, curPos, EndPos - curPos)));
  
 curPos := PosFrom('Качество:', HTML, EndPos);
 curPos := curPos + Length('Качество:');
 EndPos := PosFrom(#13, HTML, curPos);
 
 AddFieldValueXML('rip', Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 curPos := PosFrom('Размер:', HTML, EndPos);
 curPos := curPos + Length('Размер:');
 EndPos := PosFrom(' Mb', HTML, curPos);
 
 AddFieldValueXML('size', Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 //TODO: Video, audio
 
 curPos := PosFrom('Язык:', HTML, EndPos);
 curPos := curPos + Length('Язык:');
 EndPos := PosFrom(#13, HTML, curPos);
 
 AddFieldValueXML('langs', Trim(Copy(HTML, curPos, EndPos - curPos)));
 
 curPos := PosFrom('Субтитры:', HTML, EndPos);
 curPos := curPos + Length('Субтитры:');
 EndPos := PosFrom(#13, HTML, curPos);
 
 AddFieldValueXML('subs', Trim(Copy(HTML, curPos, EndPos - curPos)));
end;

function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
begin
 ParseMovie(HTML);
 
 Result := prFinished;
end;

begin
 Mode := smSearch; 
end.
