
//窗体打开方式
var FILE_OPEN_NORMAL = 0;
var FILE_OPEN_DIALOG = 3;
var FILE_OPEN_DOCKWND = 5;

//数字量状态
var MENU_STATE_DI_SET_VALUE = 2048;    /*人工置数*/
var MENU_STATE_DI_STOP_REF = 4096;    /*点禁止刷新 */
var MENU_STATE_DI_STOP_ALARM = 8192; /*点禁止报警*/

//模拟量状态
var MENU_STATE_AI_SET_VALUE = 1024;    /*人工置数*/
var MENU_STATE_AI_STOP_REF = 1048576;    /*点禁止刷新 */
var MENU_STATE_AI_STOP_ALARM = 2097152; /*点禁止报警*/

var g_iconAction = {};

function g_begin()
{
  SView.clicked.connect(g_clicked);
  SView.doubleClicked.connect(g_doubleClicked);
  
  if(JSON.stringify(g_iconAction) == "{}")
  {
    var str = SDAO.readFileData("../../data/model/iconAction.json");
    g_iconAction = JSON.parse(str);  
  }
}

function g_clicked(itemCout, msgStr)
{
  openDialog(itemCout, msgStr, 0);
}

function g_doubleClicked(itemCout, msgStr)
{
  openDialog(itemCout, msgStr, 1);
}

function openDialog(itemCout, msgStr, trigger)
{
  var context = SApp.getAppContext();
  if(context != 1)
  {
    return;
  }

  if (itemCout > 0)
  {
    var tags = msgStr.split('^');
    if(tags.length < 10)
    {
      //showMessageDialog("双击信息格式不对");
      return;
    }

    var objType = tags[0];
    var objName = tags[1];
    var iconName = tags[2];
    var curValue = tags[3];
    //var domainId = tags[4];
    //var appId = tags[5];
    var keyTagName = tags[8];
    if(keyTagName=="")
    {
      return;
    }
    var dialogName = getOptDialogName(objName, keyTagName, trigger);
    if(dialogName == "")
    {
        return;
    }
    SWnd.fileOpen(dialogName, "opt", FILE_OPEN_DIALOG, keyTagName, objName);
  }
}

function g_end()
{
}

function needClose()
{
  return 1;
}

function showMessageDialog(content, title)
{
  var dlg = create("MessageBox");
  dlg.warning(content, 0, 0, 0, title);
}

function showAcceptDialog(content, title)
{
  var dlg = create("MessageBox");
  var ret = dlg.information(content, 0x00000400, 0x00400000, 0, title);
  if(ret == 0x00000400)
  {
    return "accept";
  }
  return "reject";
}

function getOptDialogName(iconName, keyTagName, trigger)
{
  var dialogName = "";
  var tableName = SView.truncTag(keyTagName, "t");
  if(iconName == "多态文本")
  {
    if(tableName == "analog")
    {
        dialogName = "界面框架/panel_analog_set.glx";
    }
    else if(tableName == "accuml")
    {
        dialogName = "界面框架/panel_accuml_ctrl.glx";
    }
    else if(tableName == "digital")
    {
        dialogName = "界面框架/panel_remote_ctrl-gdw.glx";
    }
  }
  
  if(iconName in g_iconAction)
  {
    if(g_iconAction[iconName]["Action"] == trigger)
    {
        dialogName = g_iconAction[iconName]["Panel"];
    }
  }
  return dialogName;
}

function setBit(value, bit)
{
  var ret = value | (1 << bit);
  return ret;
}

function clearBit(value, bit)
{
  var ret = value & ~(1 << bit);
  return ret;
}

function getBit(value, bit)
{
  var ret = (value >> bit) & 1;
  return ret;
}

function getUTCMsec()
{
  var date = new Date();
  var y = date.getFullYear();
  var mm = date.getMonth();
  var d = date.getDate();
  var h = date.getHours();
  var m = date.getMinutes();
  var s = date.getSeconds();
  var ret = Date.UTC(y,mm,d,h,m,s);
  return ret;
}

function getLocalMsec()
{
  var date = new Date();
  var ret = date.getTime();
  return ret;
}

function isControlPointTag(keyTagName)
{
    var tableName = SView.truncTag(keyTagName, "t");
    var ctrlPointTag = SView.truncTag(keyTagName, "key");
    var ctrlSubName = SView.truncTag(keyTagName, "a");
    var ctrlAppName = SDAO.getAppNameBySubName(ctrlSubName);
    var loactionName = SView.truncTag(keyTagName, "d");
    var ctrlDomainId = SDAO.getDomainIdByLocation(loactionName);
    
    if(tableName == "accuml")
    {
        return false;
    }
    
    var isControl = SDAO.rdbNetGetByKey(ctrlAppName, tableName, ctrlPointTag, "is_control", ctrlDomainId);
    if (isControl[0] < 0)
    {
        return false;
    }

    return isControl[1] == 1;
}