/// <reference path="Extjs_Intellisense.js" />
/// <reference path="src/data/XmlStore.js" />
/// <reference path="addjs/Base.js" />
/// <reference path="addjs/XmlTreeLoader.js" />

//
var alrmxmlreader=new Ext.data.XmlReader({
               record: 'Response'
           }, [
            {name: 'Code', mapping: '@Code'},
            {name: 'DevId', mapping: '@DevId'},
            {name: 'AlarmType', mapping: '@Type'},
            {name: 'AlarmTime', mapping: '@Timestamp'}
            ]);
            
var alarmxml='<Response Code="204" DevId="755000012" Timestamp="2147706207" Type="259" IsRaise="1" AlarmLevel="1" Description="motion detect"  Result ="0"/>';
var alarmfilegrid=null;
var alarmxmlStore = new Ext.data.Store(
{appendData:true
,proxy:new Ext.data.StringProxy({xml:alarmxml,reader:alrmxmlreader})
,reader:alrmxmlreader
//,sortInfo:{field:"OrderId",direction:"DESC"}
});

function GetDevId(DevId)
{
    var realtree=GetRelaTimeTree();
    if(realtree)
    {
        var node=realtree.root.findChild("Id",""+DevId,true);
        if(node && node.text)
        {
            return node.text;
        }
    }
    return DevId;
}

function GetAlarmTypeName(TypeId,metaData, record, rowIndex, colIndex, store)
{
    if(typeof(TypeId)=="string")
    {
        TypeId=parseInt(TypeId);
    }
//EVENT_TYPE_NONE= alarmcode++;//,
//EVENT_TYPE_USB_PLUGIN= alarmcode++;//, //U盘插入
//EVENT_TYPE_USB_UNPLUG= alarmcode++;//, //U盘拔出
//EVENT_TYPE_SD1_PLUGIN= alarmcode++;//, //SD1插入
//EVENT_TYPE_SD1_UNPLUG= alarmcode++;//, //SD1拔出
//EVENT_TYPE_SD2_PLUGIN= alarmcode++;//, //SD2插入
//EVENT_TYPE_SD2_UNPLUG= alarmcode++;//, //SD2拔出
//EVENT_TYPE_RECORD_START= alarmcode++;//, //录像开始
//EVENT_TYPE_RECORD_FINISHED= alarmcode++;//, //录像结束
//EVENT_TYPE_RECORD_FILE_REMOVED= alarmcode++;//, //录像文件删除
//EVENT_TYPE_RECORD_DIR_REMOVED= alarmcode++;//, //录像目录删除
    
    var strAlarmType=0+TypeId;    
    var msgtype=204;
    try
    {
        msgtype=record.data["Code"];
    }catch(eee){}
    if(msgtype=="203" || msgtype==203)
    {
        switch(TypeId)
        {
            case EVENT_TYPE_USB_PLUGIN:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_USB_PLUGIN","U盘插入");
            }
            break;
            case EVENT_TYPE_USB_UNPLUG:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_USB_UNPLUG","U盘拔出");
            }
            break;
            case EVENT_TYPE_SD1_PLUGIN:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_SD1_PLUGIN","SD1插入");
            }
            break;
            case EVENT_TYPE_SD1_UNPLUG:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_SD1_UNPLUG","SD1拔出");
            }
            break;
            case EVENT_TYPE_SD2_PLUGIN:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_SD2_PLUGIN","SD2插入");
            }
            break;
            case EVENT_TYPE_SD2_UNPLUG:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_SD2_UNPLUG","SD2拔出");
            }
            break;
            case EVENT_TYPE_RECORD_START:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_RECORD_START","录像开始");
            }
            break;
            case EVENT_TYPE_RECORD_FINISHED:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_RECORD_FINISHED","录像结束");
            }
            break;
            case EVENT_TYPE_RECORD_FILE_REMOVED:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_RECORD_FILE_REMOVED","录像文件删除");
            }
            break;
            case EVENT_TYPE_RECORD_DIR_REMOVED:
            {
                strAlarmType=GetDefaultLang("lang_EVENT_TYPE_RECORD_DIR_REMOVED","录像目录删除");
            }
            break;
            default:
            {
                strAlarmType=GetDefaultLang("lang_eventstr","事件")+strAlarmType;
            }
            break;
        }
    }else
    {    
        switch(TypeId)
        {
	    case VS_ALARM_TYPE_NONE:
		    strAlarmType = TypeId;//"";
		    break;
	    case VS_ALARM_TYPE_FIRE:
		    strAlarmType =GetDefaultLang("lang_VS_ALARM_TYPE_FIRE", "火警");
		    break;
	    case VS_ALARM_TYPE_SMOKE:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_SMOKE","烟警");
		    break;
	    case VS_ALARM_TYPE_INFRARED:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_INFRARED","红外报警");
		    break;
	    case VS_ALARM_TYPE_GAS:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_GAS","气体报警");
		    break;
	    case VS_ALARM_TYPE_TEMPERATURE:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_TEMPERATURE","温度报警");
		    break;
	    case VS_ALARM_TYPE_GATING:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_GATING","门禁报警");
		    break;
	    case VS_ALARM_TYPE_MANUAL:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_MANUAL","手动报警");
		    break;
	    case VS_ALARM_TYPE_FACEDECTECT:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_FACEDECTECT","人脸检测");
		    break;
	    case VS_ALARM_TYPE_FACERECOGNIZED:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_FACERECOGNIZED","人脸识别");
		    break;
	    case VS_ALARM_TYPE_MOTIONDECTECT:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_MOTIONDECTECT","移动侦测");
		    break;
	    case VS_ALARM_TYPE_VIDEO_LOST:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_VIDEO_LOST","视频丢失");
		    break;
	    case VS_ALARM_TYPE_VIDEO_COVERED:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_VIDEO_COVERED","视频遮挡");
		    break;
	    case VS_ALARM_TYPE_STORAGE_FULL:
		    strAlarmType =GetDefaultLang("lang_VS_ALARM_TYPE_STORAGE_FULL", "存储空间不足");
		    break;
	    case VS_ALARM_TYPE_RECORD_FAILED:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_RECORD_FAILED","录像失败");
		    break;
	    case VS_ALARM_TYPE_IO_LOW2HIGH:
		    strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_IO_LOW2HIGH","IO由低变高");
		    break;
	    case VS_ALARM_TYPE_IO_HIGH2LOW:
	        {
		        strAlarmType = GetDefaultLang("lang_VS_ALARM_TYPE_IO_HIGH2LOW","IO由高变低");
		    }
		    break;
		case 266:
		    {
		        strAlarmType = GetDefaultLang("lang_FROMH2L","IO由高变低");
		    }
		break;
	    default:
	        {
                strAlarmType=GetDefaultLang("lang_alarmstr","报警")+strAlarmType;
		        break;
	        }
        }
    }
    return strAlarmType;
}

function FormatTime(TimeStamp)
{
    if(typeof(TimeStamp)=="string")
    {
        TimeStamp=parseInt(TimeStamp);
    }
    var date=new Date(TimeStamp*1000);
    return date.format("y-m-d H:i:s");
}

function GetAlarmResultGrid()
{
    if(alarmfilegrid==null)
    {       
        alarmfilegrid =new Ext.grid.GridPanel(        
        {
            store: alarmxmlStore,
            columns: 
            [
                {header: GetDefaultLang("lang_devicename",'设备名称'), width: 80, sortable: true,renderer:GetDevId, dataIndex: 'DevId'},
                {header: GetDefaultLang("lang_alarmtype",'报警类型'), width: 80, sortable: true,renderer:GetAlarmTypeName, dataIndex: 'AlarmType'},
                {header: GetDefaultLang("lang_alarmdate",'报警时间'), width: 115, sortable: true,renderer:FormatTime, dataIndex: 'AlarmTime'}
            ],
            stripeRows: true,
            width:240,
            height:270
        } 
        );        
    }
    return alarmfilegrid;
}


var nowalarmxmlidx=13;
var nowxmlarray=new Array();
var nowtimeIdx=0;


function ShowAlarmByXml(xml)
{
    try
    {
        var doc=CreateXml(xml);
        if(doc==null || doc==undefined )
        {
            WriteWebLog("无法解释ShowAlarmByXml XML内容:"+xml);
            return false;
        }
        var devId=doc.getAttribute("DevId");//设备ID;
        var allowalarm=GetRightByDevId(devId,"alarm");
        if(allowalarm!="")
        {//没有告警权限
            return false;
        }
    }catch(eevvv)
    {
        
    }
    nLastRecvAlarmTime=new Date().getTime();
    haveRecvAlarmData=true;
    nowtimeIdx++;
    if(nowtimeIdx<0)
    {
        nowtimeIdx=0;
    }
    nowxmlarray[nowalarmxmlidx]=xml.replace("Timestamp","  OrderId=\""+nowtimeIdx +"\"  Timestamp");
    nowalarmxmlidx--;
    if(nowalarmxmlidx<=0)
    {
        nowalarmxmlidx=13;
    }
    nowxmlarray[nowalarmxmlidx]="&&&&&&&&";
    return false;
}

function TimerShowAlarmMsg()
{
    if(timer_forShowAlarm)
    {
        window.clearTimeout(timer_forShowAlarm);
        timer_forShowAlarm=0;
    }
    try
    {
        if(!nowallowShowAlarm)
        {
            if(haveRecvAlarmData)
            {//如果有告警信息过来，则刷新数据
                var xmlmsg=nowxmlarray.join("");
                var ls=xmlmsg.split("&&&&&&&&");
                if(ls.length>1)
                {
                    xmlmsg="<xml>"+ls[1]+ls[0]+"</xml>";
                }else
                {
                    xmlmsg="<xml>"+xmlmsg+"</xml>"
                }
                haveRecvAlarmData=false;
                var filegrid=GetAlarmResultGrid();
                var olditem=filegrid.removeAll();
                delete olditem;
                olditem=null;
                filegrid.store.proxy.xml=xmlmsg;
                filegrid.store.load(true);
            }
            var obj=$("alarmtitlehead");
            var nowstyle=obj.className;
            
            if(new Date().getTime()-nLastRecvAlarmTime<3000)
            {//3秒后停止
                if(nowstyle=="colorblack")
                {
                    obj.className="colorred";
                }else
                {
                    obj.className="colorblack";
                }
            }else
            {
                if(nowstyle!="colorblack")
                {
                    obj.className="colorblack";
                }   
            }
        }
        
    }catch(eee){}
    timer_forShowAlarm=window.setTimeout("TimerShowAlarmMsg();",1000);
}
TimerShowAlarmMsg();