@name Robotboy655's Expression 2: COS
@inputs CS:wirelink KB:wirelink RAM:wirelink ED1:wirelink Power
@outputs IsTurnedOn
@persist Settings:table KEY OLDKEY
@trigger 

#POORLY CODED, REWRITE, DELETE//

interval(10)

if(first())
{
    CS[2041] = 1
    CS[2042] = 0
    CS[2046] = 0 #Enable
    CS[2044] = 0.2 #Size
    CS[2043] = 0.75 #Blink Rate (SECONDS)
}

if(~Power & Power == 1)
{
    if(IsTurnedOn == 0)
    {
        IsTurnedOn = 1
        Settings["frame",number] = 1
        Settings["tested",number] = 0
        Settings["test_failed",number] = 0
        Settings["ADDR",number] = 0
        Settings["ADDR2",string] = ""
        Settings["I",number] = 0
        Settings["TIME",number] = 0
        Settings["menu",number] = 0
        Settings["selection",number] = 1
        CS[2042] = 0
        CS[2041] = 1
    }
    elseif(IsTurnedOn == 1)
    {
        IsTurnedOn = 0
        CS[2042] = 0
        CS[2041] = 1
    }
}

if(IsTurnedOn == 1)
{
    if(Settings["frame",number] == 1)
    {
        CS[2042] = 0
        CS:writeString("robotboy655's Operating System",0,0,9)
        CS:writeString("HARDWARE & MEMORY TESTING\n------------------------------",0,2)
        if(Settings["tested",number] == 0)
        {
            CS:writeString("KEYBOARD:",0,4)
            if(KB:entity():type() == "")
            {
                CS:writeString("Not Connected",10,4,900)
                Settings["test_failed",number] = 1
            }
            elseif(KB:entity():type() != "gmod_wire_keyboard")
            {
                CS:writeString("Not Found",10,4,900)
                Settings["test_failed",number] = 2
            }
            elseif(KB:entity():type() == "gmod_wire_keyboard")
            {
                CS:writeString("Connected",10,4,90)
            }
            Settings["tested",number] = 1
        }
        if(Settings["tested",number] == 1)
        {
            CS:writeString("RAM:",0,5)
            if(RAM:entity():type() == "")
            {
                CS:writeString("Not Connected",5,5,900)
                Settings["test_failed",number] = 3
                Settings["tested",number] = 2
            }
            elseif(RAM:entity():type() == "gmod_wire_gate" | RAM:entity():type() == "gmod_wire_datasocket")
            {
                CS:writeString("Connected",5,5,90)
                RAM[Settings["I",number]] = 10
                if(RAM[Settings["I",number]] == 10)
                {
                    if(Settings["ADDR",number] >= 1024)
                    {
                        Settings["ADDR2",string] = floor(Settings["ADDR",number] / 1024,1) + " KB"
                    }
                    else
                    {
                        Settings["ADDR2",string] = Settings["ADDR",number] + " B"
                    }
                    CS:writeString("[" + Settings["ADDR2",string] + "]   ",15,5)
                    RAM[Settings["I",number]] = 0
                    Settings["ADDR",number] = Settings["ADDR",number] + 1
                    Settings["I",number] = Settings["I",number] + 1
                    if(Settings["ADDR",number] >= 1024)
                    {
                        Settings["ADDR2",string] = floor(Settings["ADDR",number] / 1024,1) + " KB"
                    }
                    else
                    {
                        Settings["ADDR2",string] = Settings["ADDR",number] + " B"
                    }
                    if(Settings["ADDR",number] == 1024){Settings["tested",number] = 2 CS:writeString("[" + Settings["ADDR2",string] + "]   ",15,5,90)}
                }
                else
                {
                    if(Settings["ADDR",number] >= 1024)
                    {
                        Settings["ADDR2",string] = floor(Settings["ADDR",number] / 1024,1) + " KB"
                    }
                    else
                    {
                        Settings["ADDR2",string] = Settings["ADDR",number] + " B"
                    }
                    if(Settings["ADDR",number] < 64)
                    {
                        CS:writeString("[" + Settings["ADDR2",string] + "]   ",15,5,900)
                        Settings["test_failed",number] = 4
                    }
                    else
                    {
                        CS:writeString("[" + Settings["ADDR2",string] + "]   ",15,5,90)
                    }
                    Settings["tested",number] = 2
                }
            }
        }
        elseif(Settings["tested",number] == 2)
        {
            CS:writeString("HDD:",0,6)
            CS:writeString("Not Avaible",5,6,900) #TODO: Add HDD Capacity
            Settings["tested",number] = 3
        }
        elseif(Settings["tested",number] == 3)
        {
            CS:writeString("ED:",0,7)
            if(ED1:entity():type() == "")
            {
                CS:writeString("Not Connected",4,7,900)
            }
            elseif(ED1:entity():type() == "gmod_wire_rfid_reader_beam")
            {
                CS:writeString("RFID Beam Reader",4,7,90)
            }
            elseif(ED1:entity():type() == "gmod_wire_keyboard")
            {
                CS:writeString("Keyboard",4,7,90)
            }
            elseif(ED1:entity():type() == "gmod_wire_datasocket")
            {
                CS:writeString("Data Socket",4,7,90)
            }
            elseif(ED1:entity():type() == "gmod_wire_consolescreen")
            {
                CS:writeString("Console Screen",4,7,90)
            }
            elseif(ED1:isHiSpeed())
            {
                CS:writeString("Hi-Speed Device Connected",4,7,90)
            }
            else
            {
                CS:writeString("Unknown Device Connected",4,7,900)
            }
            Settings["tested",number] = 4
        }
        if(Settings["tested",number] == 4)
        {
            if(Settings["test_failed",number] >= 1)
            {
                CS:writeString("ERROR REPORT\n------------------------------\nERROR CODE: " + Settings["test_failed",number],0,9)
                if(Settings["test_failed",number] == 1)
                {              
                    CS:writeString("Caused By: Not connected.\nSolution: Connect keyboard.",0,12)
                }
                elseif(Settings["test_failed",number] == 2)
                {
                    CS:writeString("Caused By: Wrong connection.\nSolution: Connect keyboard.",0,12)
                }
                elseif(Settings["test_failed",number] == 3)
                {
                    CS:writeString("Caused By: Not connected.\nSolution: Connect RAM gate.",0,12)
                }
                elseif(Settings["test_failed",number] == 4)
                {
                    CS:writeString("Caused By: Low RAM memory.\nSolution: Connect 64 byte RAM.",0,12)
                }
                else
                {
                    CS:writeString("Caused By: Unknown error code.\nSolution: Unknown error code.",0,12)
                }
        
                CS:writeString("PRESS ANY KEY TO RESRAT rOS",0,17)
        
                if(KB["Memory",number])
                {
                    IsTurnedOn = 1
                    Settings["frame",number] = 1
                    Settings["tested",number] = 0
                    Settings["test_failed",number] = 0
                    Settings["ADDR",number] = 0
                    Settings["ADDR2",string] = ""
                    Settings["I",number] = 0
                    Settings["TIME",number] = 0
                    Settings["menu",number] = 0
                    Settings["selection",number] = 1
                    CS[2042] = 0
                    CS[2041] = 1
                }
            }
            else
            {
                CS:writeString("PRESS DELETE TO ENTER SETUP",0,17)
                if(Settings["TIME",number] == 0)
                {
                    Settings["TIME",number] = curtime() + 5
                }
                if(curtime() < Settings["TIME",number] & KB["Memory",number] == 148)
                {
                    Settings["frame",number] = 2
                    CS[2041] = 1
                }
            }
        }
        if(curtime() > Settings["TIME",number] & Settings["TIME",number] != 0)
        {
            Settings["frame",number] = 3
            CS[2041] = 1
        }
    }
    elseif(Settings["frame",number] == 2)
    {
        CS[2042] = 9

        if(Settings["menu",number] == 0)
        {
            CS:writeString("robotboy655's Operating System",0,0)

            CS:writeString("Debugging                Setup",0,2)
            CS:writeString("------------------------------",0,3)
            CS:writeString("> RAM",0,4,999,Settings["selection",number] == 1 ? 666 : 9)
            CS:writeString("Colors <",22,4,999,Settings["selection",number] == 3 ? 666 : 9)
            CS:writeString("Cursor <",22,5,999,Settings["selection",number] == 4 ? 666 : 9)
            

            CS:writeString("Misc",0,10)
            CS:writeString("------------------------------",0,11)
            CS:writeString("> Exit",0,12,999,Settings["selection",number] == 2 ? 666 : 9)
            
            KEY = KB["Memory",number]

            if(KEY != OLDKEY & KEY == 18 & Settings["selection",number] != 4)
            {
                Settings["selection",number] = Settings["selection",number] + 1
            }
            elseif(KEY != OLDKEY & KEY == 17 & Settings["selection",number] != 1)
            {
                Settings["selection",number] = Settings["selection",number] - 1
            }
            elseif(KEY != OLDKEY & KEY == 13)
            {
                if(Settings["selection",number] == 2)
                {
                    Settings["frame",number] = 3
                    Settings["selection",number] = 1
                    Settings["menu",number] = 0
                    CS[2041] = 1
                }
                else
                {
                    Settings["menu",number] = Settings["selection",number]
                    Settings["selection",number] = 1
                    CS[2041] = 1
                }
            }

            OLDKEY = KEY
        }
        if(Settings["menu",number] == 1)
        {
            CS:writeString("robotboy655's Operating System",0,0)

            CS:writeString("RAM Debugging",0,2)
            CS:writeString("------------------------------",0,3)
            if(Settings["ADDR",number] >= 1024)
            {
                CS:writeString("RAM Size: " + Settings["ADDR2",string] + " (" + Settings["ADDR",number] + " B)",0,4,999)
            }
            else
            {
                CS:writeString("RAM Size: " + Settings["ADDR2",string],0,4,999)
            }

            CS:writeString("Misc",0,10)
            CS:writeString("------------------------------",0,11)
            CS:writeString("< Back",0,12,999,Settings["selection",number] == 1 ? 666 : 9)
            
            KEY = KB["Memory",number]

            if(KEY != OLDKEY & KEY == 18 & Settings["selection",number] != 1)
            {
                Settings["selection",number] = Settings["selection",number] + 1
            }
            elseif(KEY != OLDKEY & KEY == 17 & Settings["selection",number] != 1)
            {
                Settings["selection",number] = Settings["selection",number] - 1
            }
            elseif(KEY != OLDKEY & KEY == 13)
            {
                Settings["menu",number] = Settings["selection",number] - 1
                Settings["selection",number] = 1
                CS[2041] = 1
            }

            OLDKEY = KEY
        }
        if(Settings["menu",number] == 3)
        {
            CS:writeString("robotboy655's Operating System",0,0)

            CS:writeString("Color Setup",0,2)
            CS:writeString("------------------------------",0,3)

            CS:writeString("Misc",0,10)
            CS:writeString("------------------------------",0,11)
            CS:writeString("< Back",0,12,999,Settings["selection",number] == 1 ? 666 : 9)
            
            KEY = KB["Memory",number]

            if(KEY != OLDKEY & KEY == 18 & Settings["selection",number] != 1)
            {
                Settings["selection",number] = Settings["selection",number] + 1
            }
            elseif(KEY != OLDKEY & KEY == 17 & Settings["selection",number] != 1)
            {
                Settings["selection",number] = Settings["selection",number] - 1
            }
            elseif(KEY != OLDKEY & KEY == 13)
            {
                Settings["menu",number] = Settings["selection",number] - 1
                Settings["selection",number] = 3
                CS[2041] = 1
            }

            OLDKEY = KEY
        }
        if(Settings["menu",number] == 4)
        {
            CS:writeString("robotboy655's Operating System",0,0)

            CS:writeString("Cursor Setup",0,2)
            CS:writeString("------------------------------",0,3)

            CS:writeString("Misc",0,10)
            CS:writeString("------------------------------",0,11)
            CS:writeString("< Back",0,12,999,Settings["selection",number] == 1 ? 666 : 9)
            
            KEY = KB["Memory",number]

            if(KEY != OLDKEY & KEY == 18 & Settings["selection",number] != 1)
            {
                Settings["selection",number] = Settings["selection",number] + 1
            }
            elseif(KEY != OLDKEY & KEY == 17 & Settings["selection",number] != 1)
            {
                Settings["selection",number] = Settings["selection",number] - 1
            }
            elseif(KEY != OLDKEY & KEY == 13)
            {
                Settings["menu",number] = Settings["selection",number] - 1
                Settings["selection",number] = 4
                CS[2041] = 1
            }

            OLDKEY = KEY
        }
    }
    elseif(Settings["frame",number] == 3)
    {
        CS[2042] = 0
        CS:writeString("rOS started sucesfully...",0,0)
        #KEY = KB["Memory",normal]
        #CS:writeString("KB: " + KEY + " (" + toChar(KEY) +  ") A:" + A + "      ",0,1)
        #CS:writeString("" + toChar(KEY),A,2)
        #if(~KEY){A++}
    }
}
else
{
    CS[2041] = 1
    CS[2042] = 0
}
