require 'Cocos2d'

Sky = {}
Sky.colours = {
    [0] = { 0, 0, 0, 0, 0, 0 },
    [1] = { 96, 96, 255, 144, 144, 255 },
    [2] = { 96, 96, 255, 144, 144, 255 },
    [3] = { 96, 96, 255, 144, 144, 255 },
    [4] = { 96, 96, 255, 144, 144, 255 },
    [5] = { 96, 96, 255, 144, 144, 255 },
    [6] = { 96, 96, 255, 144, 144, 255 },
    [7] = { 96, 96, 255, 144, 144, 255 },
    [8] = { 96, 96, 255, 144, 144, 255 },
    [9] = { 96, 96, 255, 144, 144, 255 },
    [10] = { 96, 96, 255, 144, 144, 255 },
    [11] = { 96, 96, 255, 144, 144, 255 },
    [12] = { 96, 108, 255, 120, 168, 255 },
    [13] = { 96, 96, 255, 144, 144, 255 },
    [14] = { 96, 96, 255, 144, 144, 255 },
    [15] = { 96, 96, 255, 144, 144, 255 },
    [16] = { 0, 0, 0, 0, 0, 0 },
    [17] = { 64, 64, 216, 108, 108, 216 },
    [18] = { 96, 96, 255, 144, 144, 255 },
    [19] = { 96, 96, 255, 144, 144, 255 },
    [20] = { 96, 96, 255, 144, 144, 255 },
    [21] = { 96, 96, 255, 144, 144, 255 },
    [22] = { 96, 96, 255, 144, 144, 255 },
    [23] = { 96, 96, 255, 144, 144, 255 },
    [24] = { 0, 0, 0, 0, 0, 0 }
}

function Sky.create(self)
    local layer = cc.LayerGradient:create(cc.c4b(0, 0, 0, 255), cc.c4b(0, 0, 0, 255))
    layer.update = function(self)
        -- http://www.lua.org/pil/22.1.html
        local d = os.date('*t')
        local hr = d.hour
        -- if current is 4:45:00, then hr=4, hrr=0.75
        local hrr = d.min / 60 + d.sec / 3600
        local t = {}
        for i = 1, 6 do
            t[i] = Sky.colours[hr][i] * (1-hrr) + Sky.colours[hr+1][i] * hrr
        end
        self:setStartColor(cc.c3b(t[1], t[2], t[3]))
        self:setEndColor(cc.c3b(t[4], t[5], t[6]))
    end
    layer:update()
    return layer
end
