
qRom240416 = {}

function qRom240416.validateData(data)
    
    if (tonumber(data) == nil) then
        return 1
    else
        local tempLine = tonumber(data)
        if (tempLine ~= math.floor(tempLine)) then
            return 2
        else
            return 0
        end
    end
end


----------------------------------------------------------------


function qRom240416.generate(locX,locY,sizeX,sizeY,ori1,ori2,data,eleId,dataSize)

    -- all data must be in a number
    -- all data must be seperated by \n
    
    --ori1: Left, Right, Up, Down
    --ori2: Left/Up, Right/Down

    tpt.log("Qn-ROM-Builder: generating...")

    local tmp
    local tmpX = locX
    local tmpY = locY
    local offsetY = 0

    for ndx = 0, dataSize-1 do

        tmp = data[dataSize - ndx]
        offsetY = offsetY + 1
        if (offsetY >= 46) then
            offsetY = 0
            locX = locX + 0
        end
        tmpY = locY + offsetY
        tmpX = locX
        if (offsetY % 2 == 0) then
            tmpX = tmpX + 8
        end
        sim.partKill(tmpX, tmpY)
        local p = sim.partCreate(-3, tmpX, tmpY, eleId)
        if eleId == 0x7D then
            sim.partProperty(p, sim.FIELD_TMP, 6)
        elseif eleId == 0x1F then
            sim.partProperty(p, sim.FIELD_LIFE, 0)
            sim.partProperty(p, sim.FIELD_TEMP, 295.15)
            sim.partProperty(p, sim.FIELD_VX, 0)
            sim.partProperty(p, sim.FIELD_VY, 0)
            sim.partProperty(p, sim.FIELD_X, tmpX-1)
        elseif eleId == 0x7F then
            sim.partProperty(p, sim.FIELD_LIFE, 0xFFFF)
        end
        sim.partProperty(p, sim.FIELD_CTYPE, tmp)
        ndx = ndx + 1
    end

    return true
end


----------------------------------------------------------------


function qRom240416.input()
    local dataPath = ""
    local locX
    local locY
    locX, locY = sim.adjustCoords(tpt.mousex,tpt.mousey)
    local sizeX
    local sizeY = 46
    local defaultValue = "0x20000000"
    local ori1 = 3  --Left, Right, Up, Down
    local ori2 = 1  --Left/Up, Right/Down
    local elem = 1
    local eleId = 0x1F

    local genWindow  = Window:new  ( -1, -1,320,160)
    local title      = Label:new   ( 10, 10,300, 17, "~/ Qn ROM Bulider \\~")
    local locLabel   = Label:new   ( 10, 32, 70, 17, "Starting point: ")
    local locXBox    = Textbox:new ( 85, 32, 34, 17, locX, "x coord")
    local locYBox    = Textbox:new (125, 32, 34, 17, locY, "y coord")
    local sizeLabel  = Label:new   (176, 32, 50, 17, "ROM size:")
    local sizeXBox   = Textbox:new (236, 32, 34, 17, sizeX, "width")
    local sizeYBox   = Textbox:new (276, 32, 34, 17, sizeY, "height")
    local pathLabel  = Label:new   ( 10, 54, 70, 17, "Data file path: ")
    local pathBox    = Textbox:new ( 85, 54,205, 17, dataPath, "ex) folder/file.txt")
    local testBtn    = Button:new  (293, 54, 17, 17, "뷁")
    local oriLabel1  = Label:new   ( 10, 76, 70, 17, "Orientation:")
    local oriBox1    = Button:new  ( 85, 76, 40, 17, "Right")
    local oriLabel2  = Label:new   (130, 76, 20, 17, "->")
    local oriBox2    = Button:new  (155, 76, 40, 17, "Up")
    local elemLabel  = Label:new   (210, 76, 50, 17, "Element type:")
    local elemBox    = Button:new  (270, 76, 40, 17, "FILT")
    local defVLabel  = Label:new   ( 10, 98, 70, 17, "Default value:")
    local defVBox    = Textbox:new ( 85, 98, 85, 17, defaultValue)
    local cancelBtn  = Button:new  (180, 98, 60, 17, "Cancel")
    local confirmBtn = Button:new  (250, 98, 60, 17, "Confirm")
    local outputLabel= Label:new   ( 10,120,310, 17, "Message from the script:             Made by 쀒뚫끢쮅똹뭜쏔")
    local outputText = Label:new   ( 10,137,310, 17)

    local data = {}
    local dataSize = 0

    local terminate = function()interface.closeWindow(genWindow)end
	cancelBtn:action(terminate)

    local tryGen = function()
        
        locX = locXBox:text()
        locY = locYBox:text()
        sizeX = 46
        sizeY = 1
        dataPath = pathBox:text()
        defaultValue = 0x20000000

        outputText:text("trying to open...")
        local dataFile
        data = {}
        dataSize = 0

        dataFile = io.open(dataPath)

        if dataFile == nil then
            outputText:text("file not found")
            return
        end
        
        for line in dataFile:lines() do
            if (line == nil) then
                data[#data+1] = defaultValue
            else
                data[#data+1] = line
            end
            dataSize = dataSize + 1
        end

        if qRom240416.generate(locX,locY,sizeX,sizeY,ori1,ori2,data,eleId,dataSize) then
            dataFile:close()
            terminate()
        end
    end
    confirmBtn:action(tryGen)


    genWindow:addComponent(title)
    genWindow:addComponent(locLabel)
    genWindow:addComponent(locXBox)
    genWindow:addComponent(locYBox)
    genWindow:addComponent(sizeLabel)
    genWindow:addComponent(sizeXBox)
    genWindow:addComponent(sizeYBox)
    genWindow:addComponent(pathLabel)
    genWindow:addComponent(pathBox)
    genWindow:addComponent(testBtn)
    genWindow:addComponent(oriLabel1)
    genWindow:addComponent(oriBox1)
    genWindow:addComponent(oriLabel2)
    genWindow:addComponent(oriBox2)
    genWindow:addComponent(elemLabel)
    genWindow:addComponent(elemBox)
    genWindow:addComponent(defVLabel)
    genWindow:addComponent(defVBox)
    genWindow:addComponent(cancelBtn)
    genWindow:addComponent(confirmBtn)
    genWindow:addComponent(outputLabel)
    genWindow:addComponent(outputText)
    genWindow:onTryExit(terminate)
    genWindow:onTryOkay(tryGen)

    interface.showWindow(genWindow)
    

end
