os.loadAPI("conf")
local blocks = conf.blocks

local str = tostring

local op = peripheral.wrap("top")

local c = {x=1088, y=64, z=36}

local world = op.getWorld(0)


local unknown = {}

local width, height = term.getSize()
local w1 = math.floor((width-1) / 2)
local w2 = math.ceil((width-1) / 2)
local h1 = math.floor((height-1) / 2)
local h2 = math.ceil((height-1) / 2)

local r = 8
local redraw = true

local function posFromScreen(x,y)
    return c.x-w1+x, c.y-h1+y
end

local mbpos = {
    [1]={x=0,y=0},
    [2]={x=0,y=0},
    [3]={x=0,y=0}
}

while true do
    local y = c.y
    if redraw then
        term.setCursorPos(1,1)
        term.clearLine()
        --term.clear()
        --term.setCursorPos(1,2)
        term.write("x=" .. str(c.x) .. ",y=" .. str(c.y) .. ",z=" .. str(c.z))
        
        for i=1, height - 1 do
            local x = c.x - h1 + i
            
            term.setCursorPos(1,i+1)
            for z = c.z - w1, c.z + w2 do
                local id = world.getBlockID(x, y, z)
                local meta = world.getMetadata(x, y, z)
                local b = tostring(id)
                if meta ~=  nil then
                    b = b .. ":" .. tostring(meta)
                end

                local bl = blocks[b]
                if not bl then
                    bl = blocks[id]
                end

                if not bl then
                    bl = blocks["unknown"]
                    table.insert(unknown, b)
                end

                if bl then
                    term.setTextColor(bl.color.f)
                    term.setBackgroundColor(bl.color.b)
                    write(bl.char)
                else
          --        write(b .. "!")
                end
            end

            term.setTextColor(colors.white)
            term.setBackgroundColor(colors.black)
        end
        redraw = false
    end

    local e, v1, v2, v3, v4, v5 = os.pullEvent()
    --print(e)
    if e == "char" then
        if v1 == "e" then
            c.y = c.y + 1
            redraw = true
        elseif v1 == "q" then
            c.y = c.y - 1
            redraw = true
        elseif v1 == "w" then
            c.x = c.x - 1
            redraw = true
        elseif v1 == "a" then
            c.z = c.z - 1
            redraw = true
        elseif v1 == "s" then
            c.x = c.x + 1
            redraw = true
        elseif v1 == "d" then
            c.z = c.z + 1
            redraw = true
        end
    elseif e == "key" then
    elseif e == "mouse_click" then
        if mbpos[v1] == nil then
            mbpos[v1] = {}
        end
        mbpos[v1].x = v2
        mbpos[v1].y = v3
    elseif e == "mouse_drag" then
        if v1 == 1 then
            if v2 < mbpos[v1].x then
                c.z = c.z + 1
                redraw = true
            elseif v2 > mbpos[v1].x then
                c.z = c.z - 1
                redraw = true
            end
            if v3 < mbpos[v1].y then
                c.x = c.x + 1
                redraw = true
            elseif v3 > mbpos[v1].y then
                c.x = c.x - 1
                redraw = true
            end
        end
        
        if mbpos[v1] == nil then
            mbpos[v1] = {}
        end
        mbpos[v1].x = v2
        mbpos[v1].y = v3
    else
        print(e, ",", v1, ",", v2, ",", v3, ",", v4, ",", v5)
    end
end

