Turtle = {}
require("movement")

function Turtle:new(t)
    t = t or {}
    setmetatable(t, self)
    self.__index = self
    return t
end

function Turtle:updateInv()
    self.inv = {}
    for slot = 1, 16 do
        self.inv[slot] = turtle.getItemDetail(slot, true)
    end
end

function Turtle:assertInvUpdate(update)
    if (update ~= false) then update = true end

    if self.inv == nil then
        update = true
    end

    assert(type(update) == "boolean","update must be of the type boolean")
    if update == true then
        self:updateInv()
    end
end

function Turtle:moveTo(target)
    target = self:resolveTarget(target)
    self:log("moving to target")

    moveTo(target[1], target[2], target[3], target[4], self.dir)

    self.x, self.y, self.z, self.dir = target[1], target[2], target[3], target[4]

    self:log("target reached")

end

function Turtle:offset(position, dir, offset)
    self:assertPositionFormat(position)
    assert(type(dir) == "number", "dir must be a number")
    assert(type(offset) == "number", "offset must be a number")
    local newPosition = self:copyPositionTable(position)
    dir = dir % 4
    if (dir == 0) then newPosition[1] = newPosition[1] - offset
    elseif (dir == 1) then newPosition[3] = newPosition[3] - offset
    elseif (dir == 2) then newPosition[1] = newPosition[1] + offset
    else newPosition[3] = newPosition[3] + offset
    end
    return newPosition
end

function Turtle:assertPositionFormat(position)
    assert(type(position) == "table", "the target is not a table")
    assert(type(position[1]) == "number" and type(position[2]) == "number" and type(position[3]) == "number", "the table doesn't contain the position")
    assert(type(position[4]) == "number", "direction is missing")
end

function Turtle:log(message)
    if self.verbose == true then
        print(message)
    end
end

function Turtle:copyPositionTable(position)
    self:assertPositionFormat(position)
    local positionClone = {}
    for i = 1, 4 do
        positionClone[i] = position[i]
    end
    return positionClone
end

function Turtle:reverseArray(a)
    local a2 = {}
    assert(type(a) == "table", "argument must be an array")
    for i = 1, #a do
        a2[#a -i + 1] = a[i]
    end
    return a2
end

function Turtle:isInInv(item, update)
    assert(type(item) == "string", "item must be a string")
    self:assertInvUpdate(update)

    local tag = self:resolveTag(item)
    if (tag == "") then
        for i = 1, 16 do
            if self:checkItemName(item, i) == true then return true end
        end
    else
        for i = 1, 16 do
            if self:checkItemTag(tag, i) == true then return true end
        end
    end
    return false
end

function Turtle:resolveTag(item)
    local tag = ""
    if (item == "sapling") then
        tag = "minecraft:saplings"
    elseif (item == "log" or item == "wood") then
        tag = "minecraft:logs"
    elseif (item == "coal") then
        tag = "minecraft:coals"
    elseif (item == "seed") then
        tag = "forge:seeds"
    end
    return tag
end

function Turtle:checkItemName(name, slot)
    assert(type(name) == "string", "tag must be a string")
    assert(type(slot) == "number" and slot > 0 and slot <= 16, "slot must be between 1 and 16")
    if self.inv[slot] == nil then
        self:log("self.inv is empty at slot "..tostring(slot))
        return false
    end
    if self.inv[slot].name == nil then
        self:log("no name found")
        return false
    end
    if name == "any" then
        return true
    end
    if self.inv[slot].name == name then
        return true
    else
        return false
    end
end

function Turtle:checkItemTag(tag, slot)
    assert(type(tag) == "string", "tag must be a string")
    assert(type(slot) == "number" and slot > 0 and slot <= 16, "slot must be between 1 and 16")
    if self.inv[slot] == nil then
        self:log("self.inv is empty at slot "..tostring(slot))
        return false
    end
    if self.inv[slot].tags == nil then
        self:log("no tags found")
        return false
    end
    if self.inv[slot].tags[tag] == true then
        return true
    else
        return false
    end
end

function Turtle:dropItemsDown(item, update)
    assert(type(item) == "string", "item must be a string")
    self:assertInvUpdate(update)

    local tag = self:resolveTag(item)
    
    if(tag == "") then
        for i = 1, 16 do
            if self:checkItemName(item, i) == true then
                turtle.select(i)
                turtle.dropDown()
            end
        end
    else
        for i = 1, 16 do
            if self:checkItemTag(tag, i) == true then
                turtle.select(i)
                turtle.dropDown()
            end
        end
    end
    
end

function Turtle:isInvEmpty(update)
    self:assertInvUpdate(update)
    for i = 1, 16 do
        if (self.inv[i] ~= nil) then
            return false
        end
    end
    return true
end

function Turtle:refuel(goBack)
    if (goBack == nil) then goBack = true end
    if(goBack == true) then
        goBack = {self.x, self.y, self.z, self.dir}
    end

    self:clearInv("coalChest")
    turtle.select(1)
    turtle.suckDown()
    turtle.refuel()

    if (goBack ~= false) then
        self:log("moving to goBack")
        self:moveTo(goBack)
    end
end

function Turtle:getFuelLevel()
    return turtle.getFuelLevel()
end