
// Original script written by Dmitry Kovaliov aka Дмитрий К.
// Organization TRAINZ.RU

include "Signal.gs"

class kdavyhodkarl isclass Signal {

Soup settings;
 StringTable ST;
 string name, number;
 string MakeProperty(string link, string text) {
  string t;
  if (text=="") {t=ST.GetString("none"); } else {t=text;}
  return HTMLWindow.MakeLink("live://property/"+link,t);
 }

   thread void MonitorSignal(void);
   void ChangeSignalEffect(void);
   Asset Red, Green, Yellow;
  
  // Инициализация
public void Init(void)
{
            inherited();
            KUID RedKuid = GetAsset().LookupKUIDTable("redsignal");
            Red = World.FindAsset(RedKuid);
            KUID GreenKuid = GetAsset().LookupKUIDTable("greensignal");
            Green = World.FindAsset(GreenKuid);
            KUID YellowKuid = GetAsset().LookupKUIDTable("yellowsignal");
            Yellow =  World.FindAsset(YellowKuid);     
        
            ChangeSignalEffect();
            MonitorSignal();
            Asset self=GetAsset();
  settings=self.GetConfigSoup().GetNamedSoup("settings-table");
  ST=self.GetStringTable();
   return;  
    }

  // Создание таблицы HTML
public string GetDescriptionHTML(void)
  { 
    

  string ret="<HTML><body>";
  string bgcol=ST.GetString("BGCOLOR");
  string bgcol2=ST.GetString("BGCOLOR2");
  ret=ret+HTMLWindow.StartTable();
  ret=ret+HTMLWindow.MakeRow(HTMLWindow.MakeCell(ST.GetString("text")));
  ret=ret+HTMLWindow.MakeRow(HTMLWindow.MakeCell(ST.GetString("textname"),bgcol)+HTMLWindow.MakeCell(MakeProperty("name",name),bgcol2));
  ret=ret+HTMLWindow.MakeRow(HTMLWindow.MakeCell(ST.GetString("textnumber"),bgcol)+HTMLWindow.MakeCell(MakeProperty("number",number),bgcol2));
  ret=ret+HTMLWindow.EndTable();
  ret=ret+"</Body></html>";
  return ret;
  }
  
// Создание имен свойств 
string GetPropertyName(string p_propertyID)
  {
    StringTable strTable = GetAsset().GetStringTable();
     if(p_propertyID[0,8] == "name")
        {
        return strTable.GetString("textname");
        }
     if(p_propertyID[0,8] == "number")
        {
        return strTable.GetString("textnumber");
        }
    return strTable.GetString1("propname", p_propertyID);
   }

// Создание таблиц свойств
string GetPropertyType(string p_propertyID)
  {
    if (p_propertyID[0,8] == "name") { return "string,0,8"; }   
    if (p_propertyID[0,8] == "number") { return "string,0,8"; }       
   return "link";
  }


void SetPropertyValue(string p_propertyID, string value)
  {
    if(p_propertyID[0,8] == "name")
        {
      name=value;
   if (name=="") {SetFXNameText("name0"," ");} else {SetFXNameText("name0",name);}
  }
   if(p_propertyID[0,8] == "number")
        {
      number=value;
   if (number=="") {SetFXNameText("name1"," ");} else {SetFXNameText("name1",number);}
  }
    }

// Запись информации в базу данных
public Soup GetProperties(void)
  { 
      Soup soup = inherited();
      Soup SignalSoup = Constructors.NewSoup();
     soup.SetNamedSoup("sigdata", SignalSoup);

     SignalSoup.SetNamedTag("name",name);
     SignalSoup.SetNamedTag("number",number);
     return soup;
  }

// Загрузка информации из базы данных
public void SetProperties(Soup soup)
  {
      inherited(soup);
      Soup SignalSoup = soup.GetNamedSoup("sigdata");
     
      name=SignalSoup.GetNamedTag("name");
      number=SignalSoup.GetNamedTag("number");
  if (name=="") {SetFXNameText("name0"," ");} else {SetFXNameText("name0",name);}
  if (number=="") {SetFXNameText("name1"," ");} else {SetFXNameText("name1",number);}
  ChangeSignalEffect();
  PostMessage(me,"kdavyhodkarl3-4","Setup",0);
   }

  // зажигание сигналов светофора
  void ChangeSignalEffect(void) {         
      
             int SigState = me.GetSignalStateEx();

             SetFXCoronaTexture("yellow", null);
             SetFXCoronaTexture("red", null);
             SetFXCoronaTexture("green", null);
      
        switch (SigState)
               {
               case EX_CAUTION:
                
               SetFXCoronaTexture ("yellow",Yellow);
               break;
                
               case EX_STOP:
               SetFXCoronaTexture("red", Red);
               break; 
                
               case EX_PROCEED:
              
              SetFXCoronaTexture ("green",Green); 
               break;
              
              case EX_ADVANCE_CAUTION:
              
              SetFXCoronaTexture ("green",Green);
                break;
                default:
                break;
               }
               }
    
  // Поиск сигналов
  thread void MonitorSignal(void) {
      Message msg;     
      wait() {        
          on "Signal", "StateChanged",msg: {                 
              if(cast<Signal>msg.src == me) {             
                  ChangeSignalEffect();                  
              }
              continue; 
          }
      }    
  }
   
};