unit uEditarEngancheFlucar;

{$mode delphi}

interface

uses
  {$IFDEF WINDOWS}
  Windows,
   {$ELSE}
  LCLType,
   {$ENDIF}
  SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,
  utilidades, Grids, uEstados, StdCtrls, ExtCtrls, Buttons, ComCtrls, uCosa,
  uConstantesSimSEE, uSalasDeJuego, xMatDefs, uBaseFormularios, uauxiliares,
  ueditor_resourcestrings, uActorNodal, ucosanubeseable, uFlucar, uGeneradores,
  uComercioInternacional, uDemandas, unodos, urawdata, Types;

const
  separadorDeLineas = '|';
  IMG_NODE_ROOT = 0;
  IMG_NODE_CLOSED = 1;
  IMG_NODE_OPEN = 2;

type

  { TEditarEngancheFlucar }

  TEditarEngancheFlucar = class(TBaseFormularios)
    bt_Cancel: TButton;
    NodosFlucar: TLabel;
    NodosSimsee: TLabel;
    sg_FlucarNodos: TStringGrid;
    bt_Guardar: TButton;
    TV_SimseeNodos: TTreeView;
    iconos3: TImageList;
    procedure bt_CancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NodosFlucarClick(Sender: TObject);
    procedure guardarChecked();
    procedure bt_GuardarClick(Sender: TObject);
    procedure sg_FlucarNodosDrawCell(Sender: TObject; aCol, aRow: Integer;
      aRect: TRect; aState: TGridDrawState);
    procedure sg_FlucarNodosMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure sg_FlucarNodosMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure sg_FlucarNodosMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

    procedure TV_SimseeNodosClick(Sender: TObject);

  public
    aBarrasFlucar: Array of Integer;
    aCodigosFlucar: Array of String;
    aTipoBarraFlucar: Array of Integer;
    CheckedFlucar: array of boolean;
    actorSimseeSelec: TActorUniNodal;
    nodosFlucarSelect: Array of Integer;
    tiposColsFlucar: Array of TTipoColumna;
    argumentos, titulos: Array of String;
    constructor Create(AOwner: TComponent; sala: TSalaDeJuego);reintroduce;

    destructor Destroy; override;

    function haycheckeadosFlucar: boolean;

    procedure inicializar_Nodos_Flucar(SG_Flucar: TStringGrid);

    procedure actualizar_Nodos_Flucar(SG_Flucar: TStringGrid);

    function getCheckedOrdenado(filaCheck: Integer): Integer;

  end;


  procedure actualizar_Nodos_Simsee(TV_Simsee: TTreeView; sala: TSalaDeJuego);

  procedure actualizar_TV_Flucar(TV_Flucar: TTreeView; sala: TSalaDeJuego);

  procedure escribirEngancheFile(archi: String; sala: TSalaDeJuego);

  procedure leerEngancheFile(archi: String; sala: TSalaDeJuego);

var
  EditarEngancheFlucar: TEditarEngancheFlucar;

implementation

{$R *.lfm}

{ TEditarEngancheFlucar }

procedure TEditarEngancheFlucar.FormCreate(Sender: TObject);
begin

end;

procedure TEditarEngancheFlucar.NodosFlucarClick(Sender: TObject);
begin

end;

procedure TEditarEngancheFlucar.bt_CancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;


procedure TEditarEngancheFlucar.sg_FlucarNodosDrawCell(Sender: TObject; aCol,
  aRow: Integer; aRect: TRect; aState: TGridDrawState);
begin
   utilidades.ListadoDrawCell(Sender, ACol, ARow, aRect, aState, tiposColsFlucar[ACol],
    nil, iconos3);
end;

procedure TEditarEngancheFlucar.sg_FlucarNodosMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  utilidades.ListadoMouseDown(Sender, Button, Shift, X, Y);
end;

procedure TEditarEngancheFlucar.sg_FlucarNodosMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  utilidades.ListadoMouseMove(Sender, Shift, X, Y, tiposColsFlucar);
end;

procedure TEditarEngancheFlucar.sg_FlucarNodosMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  res: TTipoColumna;
  fila: Integer;
begin
  res := utilidades.ListadoMouseUp(Sender, Button, Shift, X, Y, tiposColsFlucar);
  case res of
    TC_checkBox:
    begin
      fila:=getCheckedOrdenado(utilidades.filaListado - 1);

      CheckedFlucar[fila] :=
        not CheckedFlucar[fila];
      sg_FlucarNodos.Cells[3, utilidades.filaListado] :=
        BoolToStr(CheckedFlucar[fila]);
    end;
    {TC_Disabled:
    begin
      if (utilidades.colListado = 1) and (utilidades.filaListado = 0) then
        invertirSeleccion;
    end }
  end;
end;

constructor TEditarEngancheFlucar.Create(AOwner: TComponent; sala: TSalaDeJuego);
begin
  inherited Create(AOwner);
  self.sala := sala;
  actorSimseeSelec:=nil;
  SetLength(argumentos,4);
  SetLength(titulos,4);
  SetLength(tiposColsFlucar,4);

  titulos[0]:='Nodo';
  titulos[1]:='Nombre';
  titulos[2]:='Tipo';
  titulos[3]:=encabezadoCheckBox;
  tiposColsFlucar[0]:=TC_Texto;
  tiposColsFlucar[1]:=TC_Texto;
  tiposColsFlucar[2]:=TC_Texto;
  tiposColsFlucar[3]:=TC_checkBox;

  leerEngancheFile(sala.Archivo_enganche_Flucar.ArchiRef.get_archi, sala);
  utilidades.initListado(sg_FlucarNodos, titulos, tiposColsFlucar, False);
  actualizar_Nodos_Simsee(self.TV_SimseeNodos, Self.sala);
  inicializar_Nodos_Flucar(self.sg_FlucarNodos);
end;

destructor TEditarEngancheFlucar.Destroy;
begin
  leerEngancheFile(sala.Archivo_enganche_Flucar.ArchiRef.get_archi, sala);
  inherited Destroy;
  self.sala := nil;
  actorSimseeSelec:=nil;
  SetLength(argumentos,0);
  SetLength(titulos,0);
  SetLength(tiposColsFlucar,0);
  SetLength(aBarrasFlucar,0);
  SetLength(aCodigosFlucar,0);
  SetLength(CheckedFlucar,0);
  SetLength(nodosFlucarSelect,0);

end;

function TEditarEngancheFlucar.haycheckeadosFlucar: boolean;
var
  res: boolean;
  i: integer;
begin
  res := False;
  for i := 0 to high(CheckedFlucar) do
    if CheckedFlucar[i] then
    begin
      res := True;
      break;
    end;
  Result := res;
end;

function TEditarEngancheFlucar.getCheckedOrdenado(filaCheck: Integer): Integer;
var
  colCelda, i: Integer;
begin
  Result:=-1;
  colCelda:=0;
  i := 0;
  for i := 0 to Length(aBarrasFlucar) - 1 do
    if CheckedFlucar[i] then
    begin
      if filaCheck=colCelda then
      begin
         Result:= i;
         Exit;
      end;
      inc(colCelda);
    end;
  for i := 0 to Length(aBarrasFlucar) - 1 do
    if not CheckedFlucar[i] then
    begin
      if filaCheck=colCelda then
      begin
         Result:= i;
         Exit;
      end;
      inc(colCelda);
    end;
end;

procedure actualizar_Nodos_Simsee(TV_Simsee: TTreeView; sala: TSalaDeJuego);
var
  k: integer;
  a: TGenerador;
  b: TComercioInternacional;
  c: TDemanda;
  d: TNodo;
  sText: string;
  lcFatherNode: TTreeNode;
begin
  lcFatherNode := nil;

  {If nothing is selected}
  if (TV_Simsee.Selected = nil) then
  begin
    {Does a root node already exist?}
    if (TV_Simsee.Items.Count = 0) then
    begin
      {Add the root node}
      lcFatherNode := TV_Simsee.Items.AddFirst(nil, 'SimSEE');
      lcFatherNode.Selected := True;

      {Set the roots image index}
      lcFatherNode.ImageIndex := IMG_NODE_ROOT;
         {Set the roots selected index. The same image is uses
            as for the ImageIndex}
      lcFatherNode.SelectedIndex := IMG_NODE_ROOT;
    end
    else
    begin
      {There is a root, so user must first select a node}
      //MessageBeep(  -1  );
      //ShowMessage(  'Select a parent node'  );
      Exit;
    end;
  end;

  {Get a name for the new node}
  //InputQuery(  'New Node',  'Caption ?',  sText  );

  ///////////////////////////////////////////////////////////////////////////
  TV_Simsee.Items.Clear;
  lcFatherNode := TV_Simsee.Items.AddFirst(nil, 'SimSEE');
  lcFatherNode.Selected := True;

  lcFatherNode.ImageIndex := IMG_NODE_ROOT;
  lcFatherNode.SelectedIndex := IMG_NODE_ROOT;
  ///////////////////////////////////////////////////////////////////////////
  sText := 'GENERADORES';
  {Add the node as a child of the selected node}
  with TV_Simsee.Items.AddChildFirst(lcFatherNode, sText) do
  begin
    {Set the image used when the node is not selected}
    ImageIndex := IMG_NODE_CLOSED;
    {Image used when the node is selected}
    SelectedIndex := IMG_NODE_OPEN;
    MakeVisible;
    Selected := True;
  end;

  for k := 0 to sala.Gens.Count - 1 do
  begin
    a := sala.Gens[k] as TGenerador;
    sText := a.ClassName + '->' + a.Apodo;
    TV_Simsee.Items.AddChild(TV_Simsee.Selected, sText);
  end;
  TV_Simsee.GetTopParent;
  TV_Simsee.GetTopParent;


  sText := 'COMERCIO INTERNACIONAL';
  {Add the node as a child of the selected node}
  with TV_Simsee.Items.AddChild(lcFatherNode, sText) do
  begin
    {Set the image used when the node is not selected}
    ImageIndex := IMG_NODE_CLOSED;
    {Image used when the node is selected}
    SelectedIndex := IMG_NODE_OPEN;
    MakeVisible;
    Selected := True;
  end;

  for k := 0 to sala.ComercioInternacional.Count - 1 do
  begin
    b := sala.ComercioInternacional[k] as TComercioInternacional;
    sText := b.ClassName + '->' + b.Apodo;
    TV_Simsee.Items.AddChild(TV_Simsee.Selected, sText);
  end;
  TV_Simsee.GetTopParent;
  TV_Simsee.GetTopParent;


  sText := 'DEMANDAS';
  {Add the node as a child of the selected node}
  with TV_Simsee.Items.AddChild(lcFatherNode, sText) do
  begin
    {Set the image used when the node is not selected}
    ImageIndex := IMG_NODE_CLOSED;
    {Image used when the node is selected}
    SelectedIndex := IMG_NODE_OPEN;
    MakeVisible;
    Selected := True;
  end;

  for k := 0 to sala.Dems.Count - 1 do
  begin
    c := sala.Dems[k] as TDemanda;
    sText := c.ClassName + '->' + c.Apodo;
    TV_Simsee.Items.AddChild(TV_Simsee.Selected, sText);
  end;
  TV_Simsee.GetTopParent;
  TV_Simsee.GetTopParent;


  sText := 'NODOS';
  {Add the node as a child of the selected node}
  with TV_Simsee.Items.AddChild(lcFatherNode, sText) do
  begin
    {Set the image used when the node is not selected}
    ImageIndex := IMG_NODE_CLOSED;
    {Image used when the node is selected}
    SelectedIndex := IMG_NODE_OPEN;
    MakeVisible;
    Selected := True;
  end;

  for k := 0 to sala.Nods.Count - 1 do
  begin
    d := sala.Nods[k] as TNodo;
    sText := d.ClassName + '->' + d.Apodo;
    TV_Simsee.Items.AddChild(TV_Simsee.Selected, sText);
  end;
  TV_Simsee.GetTopParent;
  TV_Simsee.GetTopParent;

end;


procedure actualizar_TV_Flucar(TV_Flucar: TTreeView; sala: TSalaDeJuego);
var
  Flucar1: TFlucar;
  aDemFlucar: TRaw_Load;
  aGenFlucar: TRaw_Generator;
  i: integer;
  lcFatherNode: TTreeNode;
  sText: string;
begin

  Flucar1 := TFlucar.CreateFromArchi(Sala.Archivo_sala_Flucar.GetArchi);
  Flucar1.cargar_caso;

  TV_Flucar.Items.Clear;
  lcFatherNode := nil;
  lcFatherNode := TV_Flucar.Items.AddFirst(nil, 'Flucar');
  lcFatherNode.Selected := True;

  lcFatherNode.ImageIndex := IMG_NODE_ROOT;
  lcFatherNode.SelectedIndex := IMG_NODE_ROOT;

  sText := 'GENERADORES';
  {Add the node as a child of the selected node}
  with TV_Flucar.Items.AddChildFirst(lcFatherNode, sText) do
  begin
    {Set the image used when the node is not selected}
    ImageIndex := IMG_NODE_CLOSED;
    {Image used when the node is selected}
    SelectedIndex := IMG_NODE_OPEN;
    MakeVisible;
    Selected := True;
  end;

  for i := 0 to Flucar1.sala.Generadores.Count - 1 do
  begin
    aGenFlucar := Flucar1.sala.Generadores[i];
    if (aGenFlucar.STAT = 0) then
      Continue;
    sText := 'ID: ' + TRaw_Generator(aGenFlucar).ID + '  ' +
      TRaw_Bus(TRaw_Generator(aGenFlucar).Barra_I).Name + '  Barra: ' +
      IntToStr(TRaw_Generator(aGenFlucar).I) + '  Zona: ' +
      IntToStr(TRaw_Bus(TRaw_Generator(aGenFlucar).Barra_I).ZONE);
    TV_Flucar.Items.AddChild(TV_Flucar.Selected, sText);
  end;

  TV_Flucar.GetTopParent;
  TV_Flucar.GetTopParent;

  sText := 'DEMANDAS';
  {Add the node as a child of the selected node}
  with TV_Flucar.Items.AddChild(lcFatherNode, sText) do
  begin
    {Set the image used when the node is not selected}
    ImageIndex := IMG_NODE_CLOSED;
    {Image used when the node is selected}
    SelectedIndex := IMG_NODE_OPEN;
    MakeVisible;
    Selected := True;
  end;

  for i := 0 to Flucar1.sala.Cargas.Count - 1 do
  begin
    aDemFlucar := Flucar1.sala.Cargas[i];
    sText := 'ID: ' + TRaw_Load(aDemFlucar).ID + '  ' +
      TRaw_Bus(TRaw_Load(aDemFlucar).Barra_I).Name + '  Barra: ' +
      IntToStr(TRaw_Load(aDemFlucar).I) + '  Zona: ' +
      IntToStr(TRaw_Bus(TRaw_Load(aDemFlucar).Barra_I).ZONE);
    TV_Flucar.Items.AddChild(TV_Flucar.Selected, sText);
  end;
  TV_Flucar.GetTopParent;
  TV_Flucar.GetTopParent;


  Flucar1.Free;

end;

procedure TEditarEngancheFlucar.guardarChecked();
var
  iBarra, largo_barrasActor: Integer;
begin
  setLength(actorSimseeSelec.barras_flucar, 0);
  setLength(actorSimseeSelec.codigos_flucar, 0);
  for iBarra:=0 to Length(CheckedFlucar)-1 do
      if CheckedFlucar[iBarra] then
      begin
        largo_barrasActor:=Length(actorSimseeSelec.barras_flucar);
        setLength(actorSimseeSelec.barras_flucar, largo_barrasActor+1);
        setLength(actorSimseeSelec.codigos_flucar, largo_barrasActor+1);
        actorSimseeSelec.barras_flucar[largo_barrasActor]:= aBarrasFlucar[iBarra];
        actorSimseeSelec.codigos_flucar[largo_barrasActor]:= aCodigosFlucar[iBarra];
      end;
end;

procedure TEditarEngancheFlucar.bt_GuardarClick(Sender: TObject);
var
  archivo: String;
begin
  //Se guardan los nodos select del anteriores actores
  if actorSimseeSelec<>nil then
     guardarChecked();
  actorSimseeSelec:=nil;

  archivo:=sala.Archivo_enganche_Flucar.ArchiRef.get_archi();
  escribirEngancheFile(archivo, sala);
  ModalResult := mrOk;
end;

procedure TEditarEngancheFlucar.inicializar_Nodos_Flucar(SG_Flucar: TStringGrid
  );
var
  Flucar1: TFlucar;
  i, cantBarras: Integer;
begin

  Flucar1 := TFlucar.CreateFromArchi(Sala.Archivo_sala_Flucar.GetArchi);
  Flucar1.cargar_caso;
  cantBarras:= Flucar1.sala.Barras.Count;

  SetLength(CheckedFlucar, cantBarras);
  SetLength(aBarrasFlucar, cantBarras);
  SetLength(aTipoBarraFlucar, cantBarras);
  SetLength(aCodigosFlucar, cantBarras);

  SG_Flucar.RowCount:=1;
  for i := 0 to cantBarras - 1 do
  begin
    aBarrasFlucar[i] := TRaw_Bus(Flucar1.sala.Barras[i]).I;
    aCodigosFlucar[i] := TRaw_Bus(Flucar1.sala.Barras[i]).Name;
    aTipoBarraFlucar[i] := TRaw_Bus(Flucar1.sala.Barras[i]).IDE;
    CheckedFlucar[i] := False;
  end;
  for i := 0 to SG_Flucar.ColCount - 1 do
    utilidades.AutoSizeTypedCol(SG_Flucar, i, tiposColsFlucar[i], iconos3);

  Flucar1.Free;
end;

procedure TEditarEngancheFlucar.actualizar_Nodos_Flucar(SG_Flucar: TStringGrid);
var
  iBarra, jSelect, i, colCelda: Integer;
begin
  for iBarra:=0 to Length(aBarrasFlucar)-1 do
  begin
    CheckedFlucar[iBarra]:=False;
    if  Assigned(actorSimseeSelec) and Assigned(actorSimseeSelec.barras_flucar) then
      for jSelect:=0 to Length(actorSimseeSelec.barras_flucar)-1 do
          if aBarrasFlucar[iBarra]=actorSimseeSelec.barras_flucar[jSelect] then
             CheckedFlucar[iBarra]:=True;
  end;

  SG_Flucar.RowCount := Length(aBarrasFlucar) + 1;
  colCelda:=1;
  for i := 0 to Length(aBarrasFlucar) - 1 do
    if CheckedFlucar[i] then
    begin
      SG_Flucar.Cells[0, colCelda] := IntToStr(aBarrasFlucar[i]);
      SG_Flucar.Cells[1, colCelda] := aCodigosFlucar[i];
      SG_Flucar.Cells[2, colCelda] := IntToStr(aTipoBarraFlucar[i]);
      SG_Flucar.Cells[3, colCelda] := '1';
      inc(colCelda);
    end;
  for i := 0 to Length(aBarrasFlucar) - 1 do
    if not CheckedFlucar[i] then
    begin
      SG_Flucar.Cells[0, colCelda] := IntToStr(aBarrasFlucar[i]);
      SG_Flucar.Cells[1, colCelda] := aCodigosFlucar[i];
      SG_Flucar.Cells[2, colCelda] := IntToStr(aTipoBarraFlucar[i]);
      SG_Flucar.Cells[3, colCelda] := '0';
      inc(colCelda);
    end;
  for i := 0 to SG_Flucar.ColCount - 1 do
    utilidades.AutoSizeTypedCol(SG_Flucar, i, tiposColsFlucar[i], iconos3);

end;

procedure escribirEngancheFile(archi: String; sala: TSalaDeJuego);
var
  k: integer;
  a: TGenerador;
  b: TComercioInternacional;
  c: TDemanda;
  d: TNodo;
  jBarra, jCodigo: integer;
  farchi: textFile;
begin
  assignfile(farchi, archi);
  rewrite(farchi);
  // Escribo los generadores
  writeln(farchi, 'GENERADORES;');
  for k := 0 to sala.Gens.Count - 1 do
  begin
    a := sala.Gens[k] as TGenerador;

    Write(farchi, a.ClassName + '->' + a.Apodo);

    Write(farchi, '; | ');

    if a.barras_flucar <> nil then
      for jbarra := 0 to length(a.barras_flucar) - 1 do
        Write(farchi, '; ', a.barras_flucar[jbarra]);

    Write(farchi, '; | ');
    if a.codigos_flucar <> nil then
      for jCodigo := 0 to length(a.codigos_flucar) - 1 do
        Write(farchi, '; ', a.codigos_flucar[jCodigo]);
    Write(farchi, ';');
    writeln(farchi);
  end;
  writeln(farchi);

  // Escribo los actores Comercio internacional
  writeln(farchi, 'COMERCIO INTERNACIONAL;');
  for k := 0 to sala.ComercioInternacional.Count - 1 do
  begin
    b := sala.ComercioInternacional[k] as TComercioInternacional;

    Write(farchi, b.ClassName + '->' + b.Apodo);

    Write(farchi, '; | ');

    if b.barras_flucar <> nil then
      for jbarra := 0 to length(b.barras_flucar) - 1 do
        Write(farchi, '; ', b.barras_flucar[jbarra]);

    Write(farchi, '; | ');
    if b.codigos_flucar <> nil then
      for jCodigo := 0 to length(b.codigos_flucar) - 1 do
        Write(farchi, '; ', b.codigos_flucar[jCodigo]);
    Write(farchi, ';');
    writeln(farchi);
  end;
  writeln(farchi);

  // Escribo las demandas
  writeln(farchi, 'DEMANDAS;');
  for k := 0 to sala.Dems.Count - 1 do
  begin
    c := sala.Dems[k] as TDemanda;

    Write(farchi, c.ClassName + '->' + c.Apodo);

    Write(farchi, '; | ');

    if c.barras_flucar <> nil then
      for jbarra := 0 to length(c.barras_flucar) - 1 do
        Write(farchi, '; ', c.barras_flucar[jbarra]);

    Write(farchi, '; | ');
    if c.codigos_flucar <> nil then
      for jCodigo := 0 to length(c.codigos_flucar) - 1 do
        Write(farchi, '; ', c.codigos_flucar[jCodigo]);
    Write(farchi, ';');
    writeln(farchi);
  end;
  writeln(farchi);

  // Escribo los Nodos
  writeln(farchi, 'NODOS SIMSEE;');
  for k := 0 to sala.Nods.Count - 1 do
  begin
    d := sala.Nods[k] as TNodo;
    Write(farchi, d.ClassName + '->' + d.Apodo);
    Write(farchi, '; | ');
    Write(farchi, '; ', d.ZonaFlucar);
    Write(farchi, ';');
    writeln(farchi);
  end;

  closefile(farchi);
end;

procedure leerEngancheFile(archi: String; sala: TSalaDeJuego);
var
  fent: TextFile;
  k: integer;
  a: TGenerador;
  b: TComercioInternacional;
  c: TDemanda;
  d: TNodo;
  jBarra, jCodigo: integer;
  r: string;
  pal: string;
begin
    assignfile(fent, archi);
    reset(fent);

    // Se leen los generadores
    readln(fent, r);
    getPalHastaSep(pal, r, ';');
    for k := 0 to sala.Gens.Count - 1 do
    begin
      readln(fent, r);
      getPalHastaSep(pal, r, '->');
      getPalHastaSep(pal, r, ';');
      a := sala.BuscarPorNombre(pal, sala.listaActores) as TGenerador;
      if a = nil then
        raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);
      getPalHastaSep(pal, r, ';');
      setlength(a.barras_flucar, 100);
      getPalHastaSep(pal, r, ';');
      jbarra := 0;
      while pal <> '|' do
      begin
        a.barras_flucar[jbarra] := StrToInt(pal);
        Inc(jbarra);
        getPalHastaSep(pal, r, ';');
      end;
      setlength(a.barras_flucar, jbarra);

      setlength(a.codigos_flucar, 100);
      jcodigo := 0;
      getPalHastaSep(pal, r, ';');
      while pal <> '' do
      begin
        a.codigos_flucar[jcodigo] := pal;
        Inc(jCodigo);
        getPalHastaSep(pal, r, ';');
      end;
      setlength(a.codigos_flucar, jcodigo);
    end;

    // Se leen los actores de Comercio Internacional
    readln(fent, r);
    readln(fent, r);
    getPalHastaSep(pal, r, ';');
    for k := 0 to sala.ComercioInternacional.Count - 1 do
    begin
      readln(fent, r);
      getPalHastaSep(pal, r, '->');
      getPalHastaSep(pal, r, ';');
      b := sala.BuscarPorNombre(pal, sala.listaActores) as TComercioInternacional;
      if b = nil then
        raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);
      getPalHastaSep(pal, r, ';');
      setlength(b.barras_flucar, 100);
      getPalHastaSep(pal, r, ';');
      jbarra := 0;
      while pal <> '|' do
      begin
        b.barras_flucar[jbarra] := StrToInt(pal);
        Inc(jbarra);
        getPalHastaSep(pal, r, ';');
      end;
      setlength(b.barras_flucar, jbarra);

      setlength(b.codigos_flucar, 100);
      jcodigo := 0;
      getPalHastaSep(pal, r, ';');
      while pal <> '' do
      begin
        b.codigos_flucar[jcodigo] := pal;
        Inc(jCodigo);
        getPalHastaSep(pal, r, ';');
      end;
      setlength(b.codigos_flucar, jcodigo);
    end;

    // Se leen las demandas
    readln(fent, r);
    readln(fent, r);
    getPalHastaSep(pal, r, ';');
    for k := 0 to sala.Dems.Count - 1 do
    begin
      readln(fent, r);
      getPalHastaSep(pal, r, '->');
      getPalHastaSep(pal, r, ';');
      c := sala.BuscarPorNombre(pal, sala.listaActores) as TDemanda;
      if c = nil then
        raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);
      getPalHastaSep(pal, r, ';');
      setlength(c.barras_flucar, 100);
      getPalHastaSep(pal, r, ';');
      jbarra := 0;
      while pal <> '|' do
      begin
        c.barras_flucar[jbarra] := StrToInt(pal);
        Inc(jbarra);
        getPalHastaSep(pal, r, ';');
      end;
      setlength(c.barras_flucar, jbarra);

      setlength(c.codigos_flucar, 100);
      jcodigo := 0;
      getPalHastaSep(pal, r, ';');
      while pal <> '' do
      begin
        c.codigos_flucar[jcodigo] := pal;
        Inc(jCodigo);
        getPalHastaSep(pal, r, ';');
      end;
      setlength(c.codigos_flucar, jcodigo);
    end;



  // Se leen los nodos
  readln(fent, r);
  readln(fent, r);
  getPalHastaSep(pal, r, ';');
  for k := 0 to sala.Nods.Count - 1 do
  begin
    readln(fent, r);
    getPalHastaSep(pal, r, '->');
    getPalHastaSep(pal, r, ';');
    d := sala.BuscarPorNombre(pal, sala.listaActores) as TNodo;
    if d = nil then
      raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);
    getPalHastaSep(pal, r, ';');
    getPalHastaSep(pal, r, ';');
    d.ZonaFlucar := StrToInt(pal);
    getPalHastaSep(pal, r, ';');
  end;

  closefile(fent);

end;

procedure TEditarEngancheFlucar.TV_SimseeNodosClick(Sender: TObject);
var
  nombre, nomb_FATHER: string;
  ID_padre: integer;
  pal, r: string;
  aGen: TGenerador;
  aComInter: TComercioInternacional;
  aDem: TDemanda;
  aNodo: TNodo;
  jBarra: integer;
  Nom_Gen: string;

begin
  //Se limpian los Nodos Flucar
  sg_FlucarNodos.RowCount:=1;
  //Se guardan los nodos select del anteriores actores
  if actorSimseeSelec<>nil then
     guardarChecked();

  actorSimseeSelec:=nil;
  //Se selecciono un elemento padre
  if (TV_SimseeNodos.Selected.Level >= 2) then
  begin
    ID_padre := -1;
    nombre := TV_SimseeNodos.Selected.Text;
    r := nombre;
    nomb_FATHER := TV_SimseeNodos.Selected.GetParentNodeOfAbsoluteLevel(1).Text;
    if nomb_FATHER = 'GENERADORES' then
      ID_padre := 1;
    if nomb_FATHER = 'COMERCIO INTERNACIONAL' then
      ID_padre := 2;
    if nomb_FATHER = 'DEMANDAS' then
      ID_padre := 3;
    if nomb_FATHER = 'NODOS' then
      ID_padre := 4;

    case ID_padre of
      1:
      begin
        getPalHastaSep(pal, r, '->');
        // Tipo_Gen := pal;
        Nom_Gen := r;
        aGen := sala.BuscarPorNombre(Nom_Gen, sala.listaActores) as TGenerador;
        if aGen = nil then
          raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);

        actorSimseeSelec:=aGen;
        if Assigned(aGen.barras_flucar) and (aGen.barras_flucar<>nil) then
        begin
          SetLength(nodosFlucarSelect, length(aGen.barras_flucar));
          for jbarra := 0 to length(aGen.barras_flucar) - 1 do
            nodosFlucarSelect[jBarra]:=aGen.barras_flucar[jbarra];
        end;
      end;
      2:
      begin
        getPalHastaSep(pal, r, '->');
        aComInter := sala.BuscarPorNombre(r, sala.listaActores) as
          TComercioInternacional;
        if aComInter = nil then
          raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);

        actorSimseeSelec:=aComInter;
        if Assigned(aComInter.barras_flucar) and (aComInter.barras_flucar<>nil) then
        begin
          SetLength(nodosFlucarSelect, length(aComInter.barras_flucar));
          for jbarra := 0 to length(aComInter.barras_flucar) - 1 do
            nodosFlucarSelect[jBarra]:=aComInter.barras_flucar[jbarra];
        end;
      end;
      3:
      begin
        getPalHastaSep(pal, r, '->');
        aDem := sala.BuscarPorNombre(r, sala.listaActores) as TDemanda;
        if aDem = nil then
          raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);

        actorSimseeSelec:=aDem;
        if Assigned(aDem.barras_flucar) and (aDem.barras_flucar<>nil) then
        begin
          SetLength(nodosFlucarSelect, length(aDem.barras_flucar));
          for jbarra := 0 to length(aDem.barras_flucar) - 1 do
            nodosFlucarSelect[jBarra]:=aDem.barras_flucar[jbarra];
        end;
      end;
      4:
      begin
        getPalHastaSep(pal, r, '->');
        aNodo := sala.BuscarPorNombre(r, sala.listaActores) as TNodo;
        if aNodo = nil then
          raise Exception.Create(rs_NoEncontreElActor + ': ' + pal);

        actorSimseeSelec:=nil;
        //////////////////
        //////////////////
      end;
      else
        raise Exception.Create(
          'TFSimSEEEdit.TV_SimSEENodosClick ID_Padre inválido: ' + IntToStr(ID_Padre));
    end;
    actualizar_Nodos_Flucar(sg_FlucarNodos);
  end;

end;


end.

