/**
 * Representation of an alarm in details page.
 * @constructor
 */
function AlarmeViewModel() {
    "use strict";
    /**
     * internal fields
     */
    var mSelf = this,
        o = arguments[0] || {};

    /**
     * public properties
     */
    mSelf.Codigo = ko.observable(o.Codigo || 0);
    mSelf.CodigoLinha = ko.observable(o.CodigoLinhaGrupoAlarmes || 0);
    mSelf.CodigoRegistroTerminalOcorrenc = ko.observable(o.CodigoRegistroTerminalOcorrenc || 0);

    mSelf.Descricao = ko.observable(o.Descricao || "");
    mSelf.TerminalOcorrencia = ko.observable(o.TerminalOcorrencia || "");
    mSelf.DataHoraAlarme = ko.observable(new Date(o.DataHoraAlarme || null));
    mSelf.Enlace = ko.observable(o.Enlace || "");
    mSelf.DescricaoMotorista = ko.observable(o.DescricaoMotorista || "");
    mSelf.InformacaoAdicional = ko.observable(o.InformacaoAdicional || "");
    mSelf.Situacao = ko.observable(o.Situacao || "");
    mSelf.Data = ko.observable(o.DataHora || "");
    mSelf.Selecionado = ko.observable(false);
    mSelf.Status = ko.observable(o.Status || "P");
    mSelf.RegraOrigem = ko.observable(o.RegraOrigem || 0);
    mSelf.IdentificadorMotorista = ko.observable(o.IdentificadorMotorista || 0);

    mSelf.IsMultiTratamento = ko.observable(false);
    mSelf.TratarIndividual = ko.observable(true);

    mSelf.Visivel = ko.observable(true);

    /**
     * knockout subscribes
     */
    mSelf.Selecionado.subscribe(function () {
        var arrayName = CheckArrayName();

        return tempoRealVM.alarmeModel[arrayName].PodeSelecionar(mSelf.RegraOrigem());
    });

    /**
     * knockout computed observables
     */
    mSelf.MultiTratamento = ko.computed({
        read: function () {
            return Contains(regrasOrigensTratamentoMassa, mSelf.RegraOrigem());
        },
        write: function (value) {
            if (Contains(regrasOrigensTratamentoMassa, mSelf.RegraOrigem()))
                mSelf.IsMultiTratamento(value);
        }
    });

    /**
     * public functions
     */
    mSelf.GetSituacao = function () {
        var retorno = "",
            situacao = mSelf.Situacao();
        if (situacao != null) {
            retorno = situacao;
        }

        return retorno;
    };

    mSelf.MontarContexto = function () {
        buscarAlarmeParaVisualizar(mSelf.Codigo());
    }

    mSelf.TratarAlarme = function () {
        tempoRealVM.tratarAlarmeModel.tratar(mSelf, null);
    }

    //Inicio Integração Cortex

    mSelf.HabilitaCameras = ko.observable(o.HabilitaCortex);
    mSelf.UrlCameras = o.UrlCortex;
    mSelf.DataCortexFim = o.DataCortexFim;
    mSelf.DataCortexInicio = o.DataCortexInicio;
    mSelf.VisualizarCameras = function (data, parent) {
        window.open(mSelf.UrlCameras + parent.IdentificadorVeiculo() + "/" + mSelf.DataCortexInicio + "/" + mSelf.DataCortexFim, '_blank');
        //var dt = data.Data().split('/');
        //var dia = dt[0];
        //var mes = dt[1];

        //var hr = dt[2].split(' ');
        //var ano = hr[0];

        //var hora = hr[1].split(':');

        //var dtRes = ano + mes + dia;

        //var dtInicio = new Date(ano, mes - 1, dia, hora[0], hora[1], hora[2]);
        //dtInicio.setMinutes(dtInicio.getMinutes() - 2);

        //window.open(mSelf.UrlCameras + parent.IdentificadorVeiculo() + "/" + dtRes +
        //    mSelf.VerificarDigitos(dtInicio.getHours()) + mSelf.VerificarDigitos(dtInicio.getMinutes()) +
        //    mSelf.VerificarDigitos(dtInicio.getSeconds()) + "/" + dtRes + hora.join(''), '_blank');
    };

    //mSelf.VerificarDigitos = function (data) {
    //    data = data.toString();
    //    while (data.length < 2) {
    //        data = 0 + data;
    //    }
    //    return data;
    //};

    //Fim Integração Cortex

    /**
     * internal functions
     */
    function Contains(array, search) {
        var contains = false;
        for (var i = 0; i < array.length; i++) {
            if (typeof (array[i]) === "object") {
                for (var x in array[i])
                    if (array[i][x] == search)
                        return true;
            } else {
                if (array[i] == search)
                    return true;
            }

        }
        return false;
    }

    function CheckArrayName() {
        switch (mSelf.Status()) {
            case "P":
                return "AlarmesPendentes";
            case "I":
                return "AlarmesInvalidos";
            default:
                return "AlarmesValidos";
        }
    }

    /**
     * Initialize the object
     */
    function Init() {
        mSelf.IsMultiTratamento(mSelf.MultiTratamento());
    }

    Init();
}