
ipcRenderer.on('data-message' , function(event , data){ 
    //Can Message
    if (data[2] == 0xCA) {
        updateCANMessage(data);
        updateBOOTMessage(data);
    }
});

var updateCANMessage = function(data)
{
    //Flow Device
    var env = (data[13] + data[14]*256 + data[15]*256*256 + data[16]*256*256);
    var bytes = [];
    for (var i = 0; i < 8; i++) {
        bytes[i] = data[i + 4];
    }
    if (document.getElementById("can-filt-stop").className == "ai-cell ai-cell-success") {
        var found = 0;

        if (env == 0 && document.getElementById("can-filt-env-0").className == "ai-cell ai-cell-warning") found = 1;
        else if (env == 3 && document.getElementById("can-filt-env-3").className == "ai-cell ai-cell-warning") found = 1;
        else if (env == 4 && document.getElementById("can-filt-env-4").className == "ai-cell ai-cell-warning") found = 1;
        else if (env >= 10 && env < 266 && document.getElementById("can-filt-env-10266").className == "ai-cell ai-cell-warning") found = 1;
        else if (env >= 300 && env < 365 && document.getElementById("can-filt-env-" + env.toString(16)).className == "ai-cell ai-cell-warning") found = 1;

        if (found == 1) {
            if (data[5] >= 0 && data[5] < 9 && document.getElementById("can-filt-pag-" + data[5]).className != "ai-cell ai-cell-warning") found = 0;
        }

        if (found == 1) {
            var new_row = "<tr>";
            //Envelope
            new_row += getEnvelope(env);
            new_row += "<td>" + data[13].toString(16) + "</td>";
            new_row += "<td>" + data[14].toString(16) + "</td>";
            new_row += "<td>" + data[15].toString(16) + "</td>";
            new_row += "<td>" + data[16].toString(16) + "</td>";
            //To and from
            new_row += getFrom(env, bytes[1]);
            new_row += getTo(env, bytes[1]);
            //Data
            for (var i = 0; i < 8; i++) {
                new_row += "<td>" + bytes[i].toString(16) + "</td>";
            }
            //Details
            new_row += getDetails(env, bytes);
            new_row += "</tr>";

            document.getElementById("can-message-table").innerHTML += new_row;
        }
    }
    if (env >= 5000 && env <= 5001) {
        var page = data[5];
        if (page >= 28 && page <= 32) {
            ProccessUSBData(env, bytes);
        }
    }

    var label = "";
    var page = data[5];
    if ((env == 0 || env == 3) && (page >= 0 && page < 3)) label = "can-cnt-" + env + "-" + page;
    if ((env == 4) && (page >= 0 && page <= 2)) label = "can-cnt-" + env + "-" + page;
    if (env >= 10 && env < 300 && page == 0) label = "can-cnt-10256-0";
    if (env == 300 && (page == 1 || page == 4 || page == 7)) label = "can-cnt-12c-" + page;
    if (env > 300 && env <= 3000 && page >= 0 && page <= 8 && page != 5) label = "can-cnt-d" + Math.floor((env-299)/2) + "-" + page;

    if (label != "") {
        if (document.getElementById(label) != null) {
            var value = parseInt(document.getElementById(label).innerHTML);
            document.getElementById(label).innerHTML = value + 1;
        }
    }
}

var getEnvelope = function(env)
{
    if (env == 0) return '<td><div class="ai-cell ai-cell-warning">' + env + '</div></td>';
    if (env == 3) return '<td><div class="ai-cell ai-cell-success">' + env + '</div></td>';
    if (env == 4) return '<td><div class="ai-cell ai-cell-secondary-1">' + env + '</div></td>';
    if (env > 10 && env < 300) return '<td><div class="ai-cell ai-cell-secondary-2">' + env + '</div></td>';
    if (env >= 300 && env <= 3000) return '<td><div class="ai-cell ai-cell-info-2">' + env + '</div></td>';
    return '<td><div class="ai-cell ai-cell-danger">' + env + '</div></td>';
}

var getTo = function(env, page)
{
    if (env == 0) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 3 && page == 0) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 3 && page == 1) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 3 && page == 2) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 4 && page == 0) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 4 && page == 1) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 4 && page == 2) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env > 10 && env < 300) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 0) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 1) return '<td><div class="ai-cell ai-cell-success">FLOW</div></td>';
    if (env >= 300 && page == 2) return '<td><div class="ai-cell ai-cell-success">FLOW</div></td>';
    if (env >= 300 && page == 3) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 4) return '<td><div class="ai-cell ai-cell-info-1">FC  </div></td>';
    if (env >= 300 && page == 5) return '<td><div class="ai-cell ai-cell-info-1">FC  </div></td>';
    if (env >= 300 && page == 6) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 7) return '<td><div class="ai-cell ai-cell-info-2">HYDR</div></td>';
    if (env >= 300 && page == 8) return '<td><div class="ai-cell ai-cell-info-2">HYDR</div></td>';
    return '<td><div class="ai-cell ai-cell-danger">UNKN</div></td>';
}

var getFrom = function(env, page)
{
    if (env == 0) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 3 && page == 0) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 3 && page == 1) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 3 && page == 2) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 4 && page == 0) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 4 && page == 1) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 4 && page == 2) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env > 10 && env < 300) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env >= 300 && page == 0) return '<td><div class="ai-cell ai-cell-success">FLOW</div></td>';
    if (env >= 300 && page == 1) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 2) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 3) return '<td><div class="ai-cell ai-cell-info-1">FC  </div></td>';
    if (env >= 300 && page == 4) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 5) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 6) return '<td><div class="ai-cell ai-cell-info-2">HYDR</div></td>';
    if (env >= 300 && page == 7) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env >= 300 && page == 8) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    return '<td><div class="ai-cell ai-cell-danger">UNKN</div></td>';
}

var getDetails = function(env, data)
{
    if (env == 0) {
        if (data[1] == 0) {
            //Env 0 Page 0
            return '<td>--</td>';
        } else if (data[1] == 1) {
            //Env 0 Page 1
            var basenumber = data[4] + data[5]*256 + data[6]*256*256 + (data[7]%32)*256*256*256;
            return '<td>Base number: ' + data[4].toString(16) + "," + data[5].toString(16) + "," + data[6].toString(16) + "," + data[7].toString(16) + '</td>';
        } else if (data[1] == 2) {
            //Env 0 Page 2
            var address = data[0];
            var envelope = data[2] + data[3]*256 + data[4]*256*256 + (data[5]%32)*256*256*256;
            var folder = data[6];
            return '<td>Device: ' + address.toString(16) + ', New Envelope: ' + envelope.toString(16) + ', Folder: ' + folder + '</td>';
        }
    }
    if (env == 3) {
        if (data[1] == 0) {
            //Env 3 Page 0
            var address = data[0];
            var response = data[2]%1;
            var request = Math.floor(data[2]/4)%1;
            var reset = Math.floor(data[2]/8)%8;
            var config = Math.floor(data[2]/64)%4;
            var folder = data[6];
            var output = '<td>Device: ' + address.toString(16);
            if (response != 0) output += ', Non-Init devices respond';
            if (request != 0) output += ', Request config data';
            if (reset == 5) output += ', Reset device';
            if (config == 1) output += ', Exit config mode';
            else if (config == 2) output += ', Enter config mode if not init';
            else if (config == 3) output += ', Global enter config mode';
            output += '</td>';
            return output;
        } else if (data[1] == 1) {
            //Env 3 Page 1
            var address = data[0];
            var init = data[2]%2;
            var push = Math.floor(data[2]/2)%2;
            var output = '<td>Device: ' + address.toString(16);
            if (init == 0) output += ', Device NOT init'; else output += ', Device init';
            if (push != 0) output += ', Button pushed';
            output += '</td>';
            return output;
        } else if (data[1] == 2) {
            //Env 3 Page 2
            var address = data[2];
            var output = '<td>New device id: ' + address;
            output += '</td>';
            return output;
        }
    }
    if (env == 4) {
        if (data[1] == 0) {
            //Env 4 Page 0
            var serial = data[2];
            var start = data[7]%1;
            var reset = Math.floor(data[7]/2)%2;
            var retry = Math.floor(data[7]/4)%2;
            var output = '<td>Serial: ' + serial.toString(16);
            if (start != 0) output += ', ENTER boot mode';
            if (reset != 0) output += ', RESET device';
            if (retry != 0) output += ', RETRY boot reprogram';
            output += '</td>';
            return output;
        } else if (data[1] == 1) {
            //Env 4 Page 1
            var address = data[0];
            var init = data[2]%2;
            var push = Math.floor(data[2]/2)%2;
            var checksum = data[7];
            var output = '<td>Address: ' + data[4].toString(16) + data[3].toString(16) + data[2].toString(16);
            output += ', Data: ' + data[6].toString(16) + data[5].toString(16);
            output += ', Checksum: ' + checksum.toString(16);
            output += '</td>';
            return output;
        } else if (data[1] == 2) {
            //Env 4 Page 2
            var address = data[0];
            var success = data[7]%2;
            var last = Math.floor(data[7]/2)%2;
            var address = data[4].toString(16) + data[3].toString(16) + data[2].toString(16);
            var output = '<td>';
            if (success != 0) output += 'Program SUCCESS';
            if (last != 0) output += 'Program FAILED Last address: ' + output;
            output += '</td>';
            return output;
        }
    }
    if (env > 10 && env < 300) {
        if (data[1] == 1) {
            //Env 11-299 Page 1
            var hardware = (data[3]*256 + data[2])/10;
            var software = (data[5]*256 + data[4])/10;
            var serial = data[6];
            var output = '<td>Serial: ' + serial.toString(16);
            output += ', Hardware version: ' + hardware;
            output += ', Software version: ' + software;
            output += '</td>';
            return output;
        }
    }
    if (env >= 300) {
        if (data[1] == 0) {
            //Env 300+ Page 0
            var flowRate = data[2] + data[3]*256;
            var error = data[4]%1;
            var engaged = Math.floor(data[4]/2)%2;
            var bit2 = Math.floor(data[4]/4)%2;
            var bit3 = Math.floor(data[4]/8)%2;
            var bit4 = Math.floor(data[4]/16)%2;
            var bit5 = Math.floor(data[4]/32)%2;
            var amp = Math.floor(data[4]/64)%2;
            var water = Math.floor(data[4]/128)%2;
            var inc = data[5]%2;
            var dec = Math.floor(data[5]/2)%2;
            var voltage = data[7];
            var output = '<td>Flow status';
            output += ', Flow rate: ' + flowRate + ' mL/min';
            if (error != 0) output += ', ERROR FLAG';
            if (engaged == 0) output += ', Disengaged'; else output += ', Engaged';
            if (amp != 0) output += ', AMP FLAG';
            if (water != 0) output += ', WATER FLAG';
            if (inc != 0) output += ', Increase calibration value request';
            if (dec != 0) output += ', Decrease calibration value request';
            output += ', Voltage: ' + voltage + ' V';
            output += '</td>';
            return output;
        } else if (data[1] == 1) {
            //Env 300+ Page 1
            var flowRate = data[2] + data[3]*256;
            var bit0 = data[4]%1;
            var engaged = Math.floor(data[4]/2)%2;
            var updateFlow = Math.floor(data[4]/4)%2;
            var updateEngage = Math.floor(data[4]/8)%2;
            var replyReq = Math.floor(data[4]/16)%2;
            var bit5 = Math.floor(data[4]/32)%2;
            var bit6 = Math.floor(data[4]/64)%2;
            var bit7 = Math.floor(data[4]/128)%2;
            var manualCalib = data[5]%2;
            var updateManual = Math.floor(data[5]/2)%2;
            var output = '<td>Flow setup';
            if (updateFlow != 0) {
                output += ', Update flow rate: ' + flowRate + ' mL/min';
            }
            if (updateEngage != 0) {
                output += ', Update engage:';
                if (engaged == 0) output += ' Disengaged'; else output += ' Engaged';
            }
            if (updateManual != 0) {
                if (manualCalib == 0) output += ', Disable manual calibration';
                else output += ', Enable manual calibration';
            }
            if (replyReq != 0) output += ', Request reply';
            output += '</td>';
            return output;
        } else if (data[1] == 2) {
            //Env 300+ Page 2
            var output = '<td>Flow calibration';
            output += ', Calibration number: ' + data[6].toString(16) + data[5].toString(16) + data[4].toString(16) + data[3].toString(16) + data[2].toString(16);
            output += '</td>';
            return output;
        } else if (data[1] == 3) {
            //Env 300+ Page 3
            var pressure = data[2] + data[3]*256;
            var error = data[4]%1;
            var engaged = Math.floor(data[4]/2)%2;
            var updatePickup = Math.floor(data[4]/4)%2;
            var updatePressure = Math.floor(data[4]/8)%2;
            var bit4 = Math.floor(data[4]/16)%2;
            var bit5 = Math.floor(data[4]/32)%2;
            var amp = Math.floor(data[4]/64)%2;
            var water = Math.floor(data[4]/128)%2;
            var pulse = data[5] + data[6]*256;
            var voltage = data[7];
            var output = '<td>FC status';
            output += ', Engage status: ' + engaged;
            if (updatePressure != 0) output += ', Pressure: ' + (pressure/100) + ' Bar';
            if (updatePickup != 0) output += ', Pickup pulse value: ' + pulse;
            if (amp != 0) output += ', AMP FLAG';
            if (water != 0) output += ', WATER FLAG';
            output += ', Voltage: ' + voltage + ' V';
            output += '</td>';
            return output;
        } else if (data[1] == 4) {
            //Env 300+ Page 4
            var pressure = data[2] + data[3]*256;
            var bit0 = data[4]%1;
            var engaged = Math.floor(data[4]/2)%2;
            var updatePressure = Math.floor(data[4]/4)%2;
            var updateEngage = Math.floor(data[4]/8)%2;
            var replyReq = Math.floor(data[4]/16)%2;
            var bit5 = Math.floor(data[4]/32)%2;
            var bit6 = Math.floor(data[4]/64)%2;
            var bit7 = Math.floor(data[4]/128)%2;
            var output = '<td>FC setup';
            if (updatePressure != 0) output += ', Pressure Target: ' + (pressure/100) + ' Bar';
            if (updateEngage != 0) output += ', Update engage: ' + engaged;
            if (replyReq != 0) output += ', Request reply';
            output += '</td>';
            return output;
        } else if (data[1] == 5) {
            //Env 300+ Page 5
            var output = '<td>FC message';
            output += ', Nothing';
            output += '</td>';
            return output;
        } else if (data[1] == 6) {
            //Env 300+ Page 6
            var flowRate = data[2] + data[3]*256 + data[5]*256*256
            var error = data[4]%1;
            var engaged = Math.floor(data[4]/2)%2;
            var bit2 = Math.floor(data[4]/4)%2;
            var bit3 = Math.floor(data[4]/8)%2;
            var bit4 = Math.floor(data[4]/16)%2;
            var bit5 = Math.floor(data[4]/32)%2;
            var amp = Math.floor(data[4]/64)%2;
            var water = Math.floor(data[4]/128)%2;
            var RPM = data[6];
            var voltage = data[7];
            var output = '<td>Hyd status';
            output += ', Flow rate: ' + flowRate + ' mL/min';
            if (error != 0) output += ', ERROR FLAG';
            if (engaged == 0) output += ', Disengaged'; else output += ', Engaged';
            if (amp != 0) output += ', AMP FLAG';
            if (water != 0) output += ', WATER FLAG';
            output += ', RPM: ' + RPM + ' rev/min';
            output += ', Voltage: ' + (voltage*136/920) + ' V';
            output += '</td>';
            return output;
        } else if (data[1] == 7) {
            //Env 300+ Page 7
            var flowRate = data[2] + data[3]*256 + data[5]*256*256;
            var volume = data[6] + data[7]*256;
            var bit0 = data[4]%2;
            var engaged = Math.floor(data[4]/2)%2;
            var updateFlow = Math.floor(data[4]/4)%2;
            var updateEngage = Math.floor(data[4]/8)%2;
            var replyReq = Math.floor(data[4]/16)%2;
            var updateVolume = Math.floor(data[4]/32)%2;
            var manualControl = Math.floor(data[4]/64)%2;
            var updateManual = Math.floor(data[4]/128)%2;
            var output = '<td>Hyd setup';
            if (updateFlow != 0) output += ', Update flow rate: ' + flowRate + ' mL/min';
            if (updateVolume != 0) output += ', Target volume: ' + volume;
            if (updateEngage != 0) output += ', Update engage: ' + engaged;
            if (updateManual != 0) output += ', Manual Control: ' + manualControl;
            if (replyReq != 0) output += ', Request reply';
            output += '</td>';
            return output;
        } else if (data[1] == 8) {
            //Env 300+ Page 8
            var updatePWM = Math.floor(data[4]/4)%1;
            var output = '<td>Hyd calibration';
            if (updatePWM != 0) output += ', PWM variable: ' + data[2]; else output += 'Do nothing';
            output += '</td>';
            return output;
        }
    }
    return '<td>Unknown</td>';
}

var updateBOOTMessage = function(data)
{
    //Flow Device
    var env = (data[13] + data[14]*256 + data[15]*256*256 + data[16]*256*256);
    var bytes = [];
    for (var i = 0; i < 8; i++) {
        bytes[i] = data[i + 4];
    }
    if (document.getElementById("boot-filt-stop").className == "ai-cell ai-cell-success") {

        if (env == 4 && (bytes[1] != 1)) {
            var new_row = "<tr>";
            //Envelope
            new_row += getBOOTEnvelope(env);
            new_row += "<td>" + data[13].toString(16) + "</td>";
            new_row += "<td>" + data[14].toString(16) + "</td>";
            new_row += "<td>" + data[15].toString(16) + "</td>";
            new_row += "<td>" + data[16].toString(16) + "</td>";
            //To and from
            new_row += getBOOTFrom(env, bytes[1]);
            new_row += getBOOTTo(env, bytes[1]);
            //Data
            for (var i = 0; i < 8; i++) {
                new_row += "<td>" + bytes[i].toString(16) + "</td>";
            }
            //Details
            new_row += getBOOTDetails(env, bytes);
            new_row += "</tr>";

            document.getElementById("boot-message-table").innerHTML += new_row;
        }
        getBOOTProcess(env, bytes);
    }
}

var getBOOTEnvelope = function(env)
{
    if (env == 4) return '<td><div class="ai-cell ai-cell-secondary-1">' + env + '</div></td>';
    return '<td><div class="ai-cell ai-cell-danger">' + env + '</div></td>';
}

var getBOOTTo = function(env, page)
{
    if (env == 4 && page == 0) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 4 && page == 1) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    if (env == 4 && page == 2) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    return '<td><div class="ai-cell ai-cell-danger">UNKN</div></td>';
}

var getBOOTFrom = function(env, page)
{
    if (env == 4 && page == 0) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 4 && page == 1) return '<td><div class="ai-cell ai-cell-warning">MAIN</div></td>';
    if (env == 4 && page == 2) return '<td><div class="ai-cell ai-cell-secondary-1">PERI</div></td>';
    return '<td><div class="ai-cell ai-cell-danger">UNKN</div></td>';
}

var getBOOTDetails = function(env, data)
{
    if (env == 4) {
        if (data[1] == 0) {
            //Env 4 Page 0
            var serial = data[2];
            var start = data[7]%1;
            var reset = Math.floor(data[7]/2)%2;
            var retry = Math.floor(data[7]/4)%2;
            var output = '<td>Serial: ' + serial.toString(16);
            if (start != 0) output += ', ENTER boot mode';
            if (reset != 0) output += ', RESET device';
            if (retry != 0) output += ', RETRY boot reprogram';
            output += '</td>';
            return output;
        } else if (data[1] == 1) {
            //Env 4 Page 1
            var address = data[0];
            var init = data[2]%2;
            var push = Math.floor(data[2]/2)%2;
            var checksum = data[7];
            var output = '<td>Address: ' + data[4].toString(16) + data[3].toString(16) + data[2].toString(16);
            output += ', Data: ' + data[6].toString(16) + data[5].toString(16);
            output += ', Checksum: ' + checksum.toString(16);
            output += '</td>';
            return output;
        } else if (data[1] == 2) {
            //Env 4 Page 2
            var address = data[0];
            var success = data[7]%2;
            var last = Math.floor(data[7]/2)%2;
            var address = data[4].toString(16) + data[3].toString(16) + data[2].toString(16);
            var output = '<td>';
            if (success != 0) output += 'Program SUCCESS';
            if (last != 0) output += 'Program FAILED Last address: ' + output;
            output += '</td>';
            return output;
        }
    }
    return '<td>Unknown</td>';
}

var getBOOTProcess = function(env, data)
{
    var page = data[1];
    //console.log(env + ":" + page + ":" + (data[2] + data[3]*256 + data[4]*256*256));
    if (env == 4) {
        if (page == 0) {
            var type = data[2].toString(16);
            var start = Math.floor(data[7])%2;
            var reset = Math.floor(data[7]/2)%2;
            var retry = Math.floor(data[7]/4)%2;

            if (start == 1) {
                document.getElementById("boot-bootmode").className = "ai-cell ai-cell-success";
                document.getElementById("boot-retry").className = "ai-cell ai-cell-secondary-2";
                if (type == 'a5') document.getElementById("boot-a5").className = "ai-cell ai-cell-success";
                if (type == 'fc') document.getElementById("boot-fc").className = "ai-cell ai-cell-success";
                if (type == 'c7') document.getElementById("boot-c7").className = "ai-cell ai-cell-success";
                document.getElementById("boot-complete").className = "ai-cell ai-cell-secondary-2";
                document.getElementById("boot-response").className = "ai-cell ai-cell-secondary-2";
                document.getElementById("boot-checksum").className = "ai-cell ai-cell-success";
                document.getElementById("boot-address").className = "ai-cell ai-cell-success";
                document.getElementById("boot-other").className = "ai-cell ai-cell-secondary-2";
                checksum_counter = 0;
                address_counter = 0;
                for (var i = 0; i < 40; i++) {
                    document.getElementById("boot-dev-" + i).className = "ai-cell ai-cell-danger";
                }
            } else if (reset == 1) {
                document.getElementById("boot-bootmode").className = "ai-cell ai-cell-secondary-2";
                if (type == 'a5') document.getElementById("boot-a5").className = "ai-cell ai-cell-secondary-2";
                if (type == 'fc') document.getElementById("boot-fc").className = "ai-cell ai-cell-secondary-2";
                if (type == 'c7') document.getElementById("boot-c7").className = "ai-cell ai-cell-secondary-2";
                document.getElementById("boot-complete").className = "ai-cell ai-cell-success";
                document.getElementById("boot-response").className = "ai-cell ai-cell-secondary-2";
            } else if (retry == 1) {
                document.getElementById("boot-bootmode").className = "ai-cell ai-cell-success";
                document.getElementById("boot-retry").className = "ai-cell ai-cell-danger";
                if (type == 'a5') document.getElementById("boot-a5").className = "ai-cell ai-cell-success";
                if (type == 'fc') document.getElementById("boot-fc").className = "ai-cell ai-cell-success";
                if (type == 'c7') document.getElementById("boot-c7").className = "ai-cell ai-cell-success";
                document.getElementById("boot-complete").className = "ai-cell ai-cell-secondary-2";
                document.getElementById("boot-response").className = "ai-cell ai-cell-secondary-2";
                document.getElementById("boot-checksum").className = "ai-cell ai-cell-success";
                document.getElementById("boot-address").className = "ai-cell ai-cell-success";
                document.getElementById("boot-other").className = "ai-cell ai-cell-secondary-2";
                checksum_counter = 0;
                address_counter = 0;
                for (var i = 0; i < 40; i++) {
                    if (document.getElementById("boot-dev-" + i).className == "ai-cell ai-cell-warning")
                    document.getElementById("boot-dev-" + i).className = "ai-cell ai-cell-danger";
                }
            }
        } else if (page == 1) {
            if (document.getElementById("boot-checksum").className == "ai-cell ai-cell-success") {
                checksum_counter ^= data[2];
                checksum_counter ^= data[3];
                checksum_counter ^= data[4];
                checksum_counter ^= data[5];
                checksum_counter ^= data[6];
                if (checksum_counter != data[7]) document.getElementById("boot-checksum").className = "ai-cell ai-cell-danger";
            }
            if (document.getElementById("boot-address").className == "ai-cell ai-cell-success") {
                if (address_counter != (data[2] + data[3]*256 + data[4]*256*256)) {
                    document.getElementById("boot-address").className = "ai-cell ai-cell-danger";
                }
                if (address_counter == 12) {
                    address_counter += 12;
                } else if (address_counter == 8174) {
                    address_counter = 12288;
                } else if (address_counter == 13576) {
                    address_counter = 17408;
                } else if (address_counter == 18812) {
                    address_counter = 24576;
                } else if (address_counter == 26172) {
                    address_counter = 3145728;
                } else if (address_counter == 3145740) {
                    address_counter = 15728660;
                } else {
                    address_counter += 2;
                }
            }
        } else if (page == 2) {
            var success = Math.floor(data[7])%2;
            var failed = Math.floor(data[7]/2)%2;
            var device = data[0] - 1;

            if (success == 1) document.getElementById("boot-dev-" + device).className = "ai-cell ai-cell-success";
            else if (failed == 1) document.getElementById("boot-dev-" + device).className = "ai-cell ai-cell-warning";
        }
    } else {
        if (document.getElementById("boot-bootmode").className == "ai-cell ai-cell-success") {
            document.getElementById("boot-other").className = "ai-cell ai-cell-danger";
        }
    }
}

var ProccessUSBData = function(env, bytes)
{    
    if (bytes[1] == 28) ProccessUSBData_28(env, bytes);
    else if (bytes[1] == 29) ProccessUSBData_29(env, bytes);
    else if (bytes[1] == 31) ProccessUSBData_31(env, bytes);
    else if (bytes[1] == 32) ProccessUSBData_32(env, bytes);
}

var ProccessUSBData_28 = function(env, bytes)
{    
    //USB
    updateFlagObject("flags-usb-com-fail", Math.floor(bytes[0])%2, "warning");
    updateFlagObject("flags-usb-bad-comm", Math.floor(bytes[0]/2)%2, "warning");
    updateFlagObject("flags-usb-disk-ful", Math.floor(bytes[0]/4)%2, "warning");
    updateFlagObject("flags-usb-file-inv", Math.floor(bytes[0]/8)%2, "warning");
    updateFlagObject("flags-usb-read-onl", Math.floor(bytes[0]/16)%2, "warning");
    updateFlagObject("flags-usb-fil-open", Math.floor(bytes[0]/32)%2, "warning");
    updateFlagObject("flags-usb-filn-inv", Math.floor(bytes[0]/64)%2, "warning");
    updateFlagObject("flags-usb-end-odat", Math.floor(bytes[0]/128)%2, "warning");
    
    updateFlagObject("flags-usb-return-s", Math.floor(bytes[2])%2, "warning");
    updateFlagObject("flags-usb-dev-onli", Math.floor(bytes[2]/2)%2, "warning");
    updateFlagObject("flags-usb-dev-dete", Math.floor(bytes[2]/4)%2, "warning");
    updateFlagObject("flags-usb-comm-set", Math.floor(bytes[2]/8)%2, "warning");
    updateFlagObject("flags-usb-data-mod", Math.floor(bytes[2]/16)%2, "warning");
    updateFlagObject("flags-usb-buff-ovf", Math.floor(bytes[2]/32)%2, "warning");
    updateFlagObject("flags-usb-expl-typ", Math.floor(bytes[2]/64)%2, "warning");
    updateFlagObject("flags-usb-reserved", Math.floor(bytes[2]/128)%2, "warning");
    
    document.getElementById("counter-usb-return-page").innerText = Math.floor(bytes[3])%8;
    updateFlagObject("flags-usb-prog-typ", Math.floor(bytes[3]/8)%2, "warning");
    document.getElementById("counter-usb-read-d-f-typ").innerText = Math.floor(bytes[3]/16)%16;
    
    document.getElementById("counter-usb-writ-d-f-typ").innerText = Math.floor(bytes[4])%16;
    document.getElementById("counter-usb-main-action").innerText = Math.floor(bytes[4]/16)%16;
    document.getElementById("counter-usb-base-action").innerText = Math.floor(bytes[5])%16;
    document.getElementById("counter-usb-comm-action").innerText = Math.floor(bytes[5]/16)%16;

    updateFlagObject("flags-usb-hal-read", Math.floor(bytes[6])%2, "warning");
    updateFlagObject("flags-usb-run--out", Math.floor(bytes[6]/2)%2, "warning");
    updateFlagObject("flags-usb-vis-allf", Math.floor(bytes[6]/4)%2, "warning");
    updateFlagObject("flags-usb-vis-pl-f", Math.floor(bytes[6]/8)%2, "warning");
    updateFlagObject("flags-usb-vis-cf-f", Math.floor(bytes[6]/16)%2, "warning");
    updateFlagObject("flags-usb-vis-pf-f", Math.floor(bytes[6]/32)%2, "warning");
    updateFlagObject("flags-usb-vis-ph-f", Math.floor(bytes[6]/64)%2, "warning");
    updateFlagObject("flags-usb-vis-pd-f", Math.floor(bytes[6]/128)%2, "warning");

    updateFlagObject("flags-usb-boot-mod", Math.floor(bytes[7])%2, "warning");

    var main_ac = Math.floor(bytes[4]/16)%16;
    var base_ac = Math.floor(bytes[5])%16;
    var comm_ac = Math.floor(bytes[5]/16)%16;
    
    for (var i = 0; i < 12; i++) {
        updateFlagObject("usb-main-action-" + i, (main_ac == i), "warning");
    }
    for (var i = 0; i < 9; i++) {
        updateFlagObject("usb-base-action-" + i, (base_ac == i), "warning");
    }
    for (var i = 0; i < 6; i++) {
        updateFlagObject("usb-command-action-" + i, (comm_ac == i), "warning");
    }
}

var ProccessUSBData_29 = function(env, bytes)
{
    document.getElementById("counter-usb-prog-device").innerText = Math.floor(bytes[0]);
    document.getElementById("counter-usb-direc-level").innerText = Math.floor(bytes[2]);
    document.getElementById("counter-usb-read-counter").innerText = Math.floor(bytes[3]);
    document.getElementById("counter-usb-page-counter").innerText = Math.floor(bytes[4]);
}

var ProccessUSBData_32 = function(env, bytes)
{
    document.getElementById("counter-usb-data-counter").innerText = bytes[0] + bytes[2]*256 + bytes[3]*256*256;
    document.getElementById("counter-usb-file-length").innerText = bytes[4] + bytes[5]*256 + bytes[6]*256*256 + bytes[7]*256*256*256;
}

var USB_RX_Buffer = [];
var USB_TX_Buffer = [];
var child_counter = 0;
var new_line = 0;
var USB_mode = 0; //0: Command, 1:File, 2:Data

var ProccessUSBData_31 = function(env, bytes)
{
    var data = '';
    var read = Math.floor(bytes[0]/128)%2;
    var count = Math.floor(bytes[0])%8;

    //ProcessUSB_RX(bytes[0]);
    if (read) {
        for (var i = 0; i < count; i++) {
            USBModeProcessPre(bytes[2 + i]);
            if (USB_mode == 0) ProcessUSB_RX(bytes[2 + i]);
            else if (USB_mode == 1) ProcessUSB_FILE(bytes[2 + i]);
            else if (USB_mode == 2) ProcessUSB_DATA(bytes[2 + i]);
            //ProcessUSB_SEC(bytes[2 + i]);
            USBModeProcessPost(bytes[2 + i]);
        }
    } else {
        for (var i = 0; i < count; i++) {
            USBModeProcessPre(bytes[2 + i]);
            if (USB_mode == 0) ProcessUSB_TX(bytes[2 + i]);
            else if (USB_mode == 1) ProcessUSB_FILE(bytes[2 + i]);
            else if (USB_mode == 2) ProcessUSB_TXDATA(bytes[2 + i]);
            //ProcessUSB_SEC(bytes[2 + i]);
            USBModeProcessPost(bytes[2 + i]);
        }
    }
}

var ProcessUSB_RX = function(orig)
{
    var data = '<a style="color: #ff7878; font-family: \'Courier New\';">' + GetCharRep(orig) + '</a>';
    ProcessUSB_GEN(orig, data, 0, 1);
}

var ProcessUSB_TX = function(orig)
{
    var data = '<a style="color: #8fff78; font-family: \'Courier New\';">' + GetCharRep(orig) + '</a>';
    ProcessUSB_GEN(orig, data, 0, 1);
}

var ProcessUSB_FILE = function(orig)
{
    var data = '<a style="color: #ffae3d; font-family: \'Courier New\';">' + GetCharRep(orig) + '</a>';
    ProcessUSB_GEN(orig, data, 0, 1);  
}

var ProcessUSB_DATA = function(orig)
{
    var data = '<a style="color: #b8c9db; font-family: \'Courier New\';"> 0x' + ('0' + orig.toString(16)).substr(-2) + '</a>';
    if (USB_mode_read_counter < 1000) {
        ProcessUSB_GEN(orig, data, 0, 1);
    } else if (USB_mode_read_counter == 1000) {
        ProcessUSB_GEN(orig, " ...", 0, 1);
        USB_mode_read_counter++;
    } else {

    }
}

var ProcessUSB_TXDATA = function(orig)
{
    var data = '<a style="color: #8fff78; font-family: \'Courier New\';"> 0x' + ('0' + orig.toString(16)).substr(-2) + '</a>';
    ProcessUSB_GEN(orig, data, 0, 1);
}

var ProcessUSB_SEC = function(orig)
{
    var data = '<a style="color: #ffffff; font-family: \'Courier New\';"> 0x' + ('0' + orig.toString(16)).substr(-2) + '</a>';
    ProcessUSB_GEN(orig, data, 1, 0);
}

var ProcessUSB_GEN = function(orig, data, col, check)
{
    if (check) ProcessUSB_ChildCounterCheck(orig);
    var count = document.getElementById("USB-data-table").children.length;
    document.getElementById("USB-data-table").children[count - 1].children[col].innerHTML += data;
    if (new_line == 1) ProcessUSB_AppendNewChild();
}

var USB_mode_comm_dir = 0;
var USB_mode_comm_opw = 0;
var USB_mode_comm_wrf = 0;
var USB_mode_comm_sek = 0;
var USB_mode_comm_rd = 0;
var USB_mode_data_counter = 0;
var USB_mode_data_back = 0;
var USB_mode_read_counter = 0;

var USBModeProcessPre = function(data)
{
    if (USB_mode == 1) {
        if (data == 0x3E) USB_mode = 0;
    } else if (USB_mode == 2) {
        if (USB_mode_data_counter > 0) {
            USB_mode_data_counter--;
        }

        if (USB_mode_comm_wrf == 3) {
            USB_mode_data_back *= 256;
            USB_mode_data_back += data;
        }
    }
}

var USBModeProcessPost = function(data)
{
    if  (USB_mode == 0) {
        //DIR
        if (USB_mode_comm_dir == 0 && USB_mode_comm_wrf == 0 && USB_mode_comm_opw == 0 && USB_mode_comm_sek == 0 && USB_mode_comm_rd == 0 && data == 1) {
            USB_mode_comm_dir = 1;
        } else if (USB_mode_comm_dir == 1) {
            if (data == 0x20) {
                USB_mode_comm_dir = 2;
            } else if (data == 13) {
                USB_mode_comm_dir = 0;
                SetUSBMode(1);
            }
        } else if (USB_mode_comm_dir == 2 && data == 13) {
            USB_mode_comm_dir = 3;
        } else if (USB_mode_comm_dir == 3 && data == 13) {
            USB_mode_comm_dir = 4;
        } else if (USB_mode_comm_dir == 4 && data == 0x20) {
            USB_mode_comm_dir = 0;
            USB_mode_data_counter = 4;
            SetUSBMode(2);
        }
        //OPW
        if (USB_mode_comm_dir == 0 && USB_mode_comm_wrf == 0 && USB_mode_comm_opw == 0 && USB_mode_comm_sek == 0 && USB_mode_comm_rd == 0 && data == 9) {
            USB_mode_comm_opw = 1;
        } else if (USB_mode_comm_opw == 1 && data == 32) {
            USB_mode_comm_opw = 2;
        } else if (USB_mode_comm_opw == 2 && data == 32) {
            USB_mode_comm_opw = 0;
            USB_mode_data_counter = 4;
            SetUSBMode(2);
        }
        //WRF
        if (USB_mode_comm_dir == 0 && USB_mode_comm_wrf == 0 && USB_mode_comm_opw == 0 && USB_mode_comm_sek == 0 && USB_mode_comm_rd == 0 && data == 8) {
            USB_mode_comm_wrf = 1;
        } else if (USB_mode_comm_wrf == 1 && data == 32) {
            USB_mode_comm_wrf = 2;
            USB_mode_data_back = 0;
            USB_mode_data_counter = 4;
            SetUSBMode(2);
        } else if (USB_mode_comm_wrf == 2 && data == 13) {
            USB_mode_comm_wrf = 0;
            USB_mode_data_counter = USB_mode_data_back;
            SetUSBMode(2);
        }
        //SEK
        if (USB_mode_comm_dir == 0 && USB_mode_comm_wrf == 0 && USB_mode_comm_opw == 0 && USB_mode_comm_sek == 0 && USB_mode_comm_rd == 0 && data == 0x28) {
            USB_mode_comm_sek = 1;
        } else if (USB_mode_comm_sek == 1 && data == 32) {
            USB_mode_comm_sek = 0;
            USB_mode_data_counter = 4;
            SetUSBMode(2);
        }
        //RD
        if (USB_mode_comm_dir == 0 && USB_mode_comm_wrf == 0 && USB_mode_comm_opw == 0 && USB_mode_comm_sek == 0 && USB_mode_comm_rd == 0 && data == 4) {
            USB_mode_comm_rd = 1;
        } else if (USB_mode_comm_rd == 1 && data == 32) {
            USB_mode_comm_rd = 2;
        } else if (USB_mode_comm_rd == 2 && data == 13) {
            USB_mode_comm_rd = 3;
            SetUSBMode(2);
        }
    } else if (USB_mode == 2) {

        USB_mode_read_counter++;
        if (USB_mode_comm_rd == 3) {
            if (data == 0x3E) {
                USB_mode_comm_rd = 4;
            }
        } else if (USB_mode_comm_rd == 4) {
            if (data == 0x0D) {
                if (USB_mode_read_counter > 1000) {
                    SetUSBMode(0);
                    ProcessUSB_RX(0x3E);
                    ProcessUSB_RX(0x0D);
                } else {
                    SetUSBMode(0);
                }
                USB_mode_comm_rd = 0;
            } else {
                USB_mode_comm_rd = 3;
            }
        } else {
            if (USB_mode_data_counter == 0) {
                SetUSBMode(0);
            }
        }

        if (USB_mode_comm_wrf == 2) {
            USB_mode_data_back *= 256;
            USB_mode_data_back += data;
        }
    }
}

var SetUSBMode = function(mode)
{
    USB_mode = mode;
    USB_mode_read_counter = 0;
}

var GetCharRep = function(data)
{
    if (data == 0) return '|NUL|';
    if (data == 1) return '|DIR|';
    if (data == 2) return '|CD|';
    if (data == 3) return '|?|';
    if (data == 4) return '|RD|';
    if (data == 7) return '|DLF|';
    if (data == 8) return '|WRF|';
    if (data == 9) return '|OPW|';
    if (data == 10) return '|CLF|';
    if (data == 13) return '|RT|';
    if (data == 0x20) return '|_|';
    if (data == 0x28) return '|SEK|';
    return String.fromCharCode(data);
}

var ProcessUSB_NewLine = function(data)
{
    child_counter = 0;
    new_line = 1;
}

var ProcessUSB_ChildCounterCheck = function(data)
{
    child_counter++;
    if (child_counter >= 16 || (USB_mode <= 1 && data == 0x0D)) ProcessUSB_NewLine();
}

var ProcessUSB_AppendNewChild = function()
{
    document.getElementById("USB-data-table").innerHTML += '<tr><td></td><td></td></tr>'; 
    new_line = 0;
}

setInterval(function() {
    
    var data = [0x00, 0x00, 0x00 ,0x00, 0x01, 0xC0, 0x5A, 0x00];
    var env = 5000;
    var result = [0x3A];
    for (var i = 0 ; i < 8; i++) {
        result[i + 1] = data[i];
    }
    result[9] = 8;
    result[10] = Math.floor(env/(256*256))%256;
    result[11] = Math.floor(env/(256*256*256))%256;
    result[12] = Math.floor(env)%256;
    result[13] = Math.floor(env/(256))%256;
    result[14] = 13;
    //console.log(result);
    var new_result = splitArray(result);
    ipcRenderer.send('can-message', new_result);
}, 550);