local c=require(script.Parent.Parent.TrelloHttp)local V=require(script.Parent.Parent.Commons)local l={fields={"\110\097\109\101","\099\111\108\111\114"}}local z={RemoteId="\105\100",Name="\110\097\109\101";Color="\099\111\108\111\114"}local H=V.generateMeta("\084\114\101\108\108\111\076\097\098\101\108")local Q local S={}function S.new(V,l,z)local H=z.Client:MakeURL("\047\108\097\098\101\108\115",{name=V,color=l,idBoard=z.RemoteId})local S=c.RequestInsist(H,c.HttpMethod.POST,"\123\125",true)local i=Q(S.Body)z._pkg.appendLabel(i)return i end Q=function(Q,S)if not S then return nil end local i={}local G={Client=Q.Client,Board=Q;Loaded=true}for c,l in pairs(z)do local H=V.getValue(S,c,z)G[c]=H i[c]=H end function G.Pull(Q,H)local S=Q.Client:MakeURL("\047\098\111\097\114\100\115\047"..Q.RemoteId,l)local N=(c.RequestInsist(S,c.HttpMethod.GET,nil,true)).Body if not N then G=nil Q=nil error("\079\079\070\033 \067\097\114\100 \104\097\115 \098\101\101\110 \100\101\108\101\116\101\100\033")end for c,l in pairs(z)do local S=V.getValue(N,c,z)if H or Q[c]==i[c]then Q[c]=S end i[c]=S end end function G.Push(l,V)local z=0 local H={}for c,Q in pairs(i)do if Q~=l[c]or V then H[c]=l[c]z=z+1 end end if z==0 then warn("\091\084\114\101\108\108\111\047\076\097\098\101\108\046\085\112\100\097\116\101\093\058 \078\111\116\104\105\110\103 \116\111 \099\104\097\110\103\101\046 \083\107\105\112\112\105\110\103\046")end local S=Q.Client:MakeURL("\047\108\097\098\101\108\115\047"..l.RemoteId,{name=H.Name;color=H.Color})c.RequestInsist(S,c.HttpMethod.PUT,"\123\125",true)for c,V in pairs(H)do i[c]=V end end function G.Delete(V)local l=Q.Client:MakeURL("\047\108\097\098\101\108\115\047"..V.RemoteId)c.RequestInsist(l,c.HttpMethod.DELETE,nil,true)G=nil end return setmetatable(G,H)end return{Public=S;Make=Q}
--[[
    Name: Label.lua
    From roblox-trello v2

    Description: Constructor for Trello labels.


    Copyright (c) 2019 Luis, David Duque

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
--]]

local HTTP = require(script.Parent.Parent.TrelloHttp)
local commons = require(script.Parent.Parent.Commons)

local fetchTable = {
    fields = {"name", "color"}
}
local indexDictionary = {
    RemoteId = "id",
    Name = "name",
    Color = "color"
}

local TrelloLabelMeta = commons.generateMeta("TrelloLabel")

-- Local function prototype to make a board based on a body dump
local makeLabel

local TrelloLabel = {}

--[[**
    Creates a new label and assigns it to the board

    @param [t:String] name The label's name. Must to be a string with a maximum of 16384 characters.
    @param [t:LabelColor] color The label's color. 
    @param [t:TrelloClient] board The board where the label will be attached to.

    @returns [t:TrelloBoard] A new TrelloBoard that was freshly created.
**--]]
function TrelloLabel.new(name, color, board)
    local commitURL = board.Client:MakeURL("/labels", {
        name = name,
        color = color,
        idBoard = board.RemoteId
    })

    local labelDump = HTTP.RequestInsist(commitURL, HTTP.HttpMethod.POST, "{}", true)
    local label = makeLabel(labelDump.Body)

    board._pkg.appendLabel(label)

    return label
end

makeLabel = function(board, data)
    if not data then
        return nil
    end

    local tracking = {}

    local trelloLabel = {
        Client = board.Client,
        Board = board,
        Loaded = true
    }

    for i, _ in pairs(indexDictionary) do
        local val = commons.getValue(data, i, indexDictionary)
        trelloLabel[i] = val
        tracking[i] = val
    end

    --[[**
        Fetches the metadata from Trello and updates the board's metadata. (Doesn't apply to lists, cards, etc.)

        @param [t:Boolean] hard Whether to overwrite changes that you did not push. (Defaults to false, which will do a soft pull)

        @returns [t:Void]
    **--]]
    function trelloLabel:Pull(hard)
        local commitURL = self.Client:MakeURL("/boards/"..self.RemoteId, fetchTable)

        local updatedData = HTTP.RequestInsist(commitURL, HTTP.HttpMethod.GET, nil, true).Body

        if not updatedData then
            -- FIXME: HANDLE THIS
            trelloLabel = nil
            self = nil
            error("OOF! Card has been deleted!")
        end

        for i, _ in pairs(indexDictionary) do
            local val = commons.getValue(updatedData, i, indexDictionary)
            if hard or self[i] == tracking[i] then
                self[i] = val
            end
            tracking[i] = val
        end
    end

    --[[**
        Pushes the label's changes to Trello.

        @param [t:Boolean] force Whether to push all changes to the board even though nothing has been changed.

        @returns [t:Void]
    **--]]
    function trelloLabel:Push(force)
        local count = 0
        local commit = {}

        for i, v in pairs (tracking) do
            if v ~= self[i] or force then
                commit[i] = self[i]
                count = count + 1
            end
        end

        if count == 0 then
            warn("[Trello/Label.Update]: Nothing to change. Skipping.")
        end

        local commitURL = board.Client:MakeURL("/labels/"..self.RemoteId, {
            name = commit.Name,
            color = commit.Color
        })

        HTTP.RequestInsist(commitURL, HTTP.HttpMethod.PUT, "{}", true)

        for i, v in pairs(commit) do
            tracking[i] = v
        end
    end

    --[[**
        Deletes the label from Trello. All garbage collection is up to the developer to perform.

        @returns [t:Void]
    **--]]
    function trelloLabel:Delete()
        local commitURL = board.Client:MakeURL("/labels/" .. self.RemoteId)

        HTTP.RequestInsist(commitURL, HTTP.HttpMethod.DELETE, nil, true)

        trelloLabel = nil
    end

    return setmetatable(trelloLabel, TrelloLabelMeta)
end

return {Public = TrelloLabel, Make = makeLabel}