ModBrowser = {}

ModPreviewItemState = 
{
    isPlayable = true,

    -- this is used only for simulation of waiting for mod download completion
    startTime = 0,
    timeToWait = 3,
    ---------------

    subscriptionRequested = false,
    waitingForDownloadFinished = false,

    prosShortInfo = {},
    prosDetailedInfo = {},
    
    playButton = 
    {
        parent = nil,

        onPressed = function(self)
            DebugPrint("Play " .. self.parent:getModNameText())
        end
    },

    subscribeButton = 
    {
        text = "Subscribe",
        parent = nil,

        onPressed = function(self)
            if not self.parent:getIsSubscribed() then
            self.parent:requestSubscribe()
            else
                self.parent:requestUnsubscribe()
            end
        end,
    },

    new = function(self)
        local o = {}
        setmetatable(o, self)
        self.__index = self

        local playButton = {}
        setmetatable(playButton, self.playButton)
        self.playButton.__index = self.playButton
        playButton.parent = o

        o.playButton = playButton

        local subscribeButton = {}
        setmetatable(subscribeButton, self.subscribeButton)
        self.subscribeButton.__index = self.subscribeButton
        subscribeButton.parent = o

         o.subscribeButton = subscribeButton

        return o
    end,

    getId = function(self)
        return self.prosShortInfo.id
    end,

    getModPreviewIcon = function(self)
        return self.prosShortInfo.icon
    end,

    getModNameText = function(self)
        return self.prosShortInfo.name
    end,

    getNumberOfLikesText = function(self)
        return self.prosShortInfo.likes
    end,

    getIsSubscribed = function(self)
        return self.prosShortInfo.subscribed ~= 0
    end,

    getModPreviewImage = function(self)
        return self.prosDetailedInfo.image
    end,

    getLastUpdateDateText = function(self)
        return self.prosDetailedInfo.updateDate
    end,

    getAuthorNameText = function(self)
        return self.prosDetailedInfo.author
    end,

    getCreationDateText = function(self)
        return self.prosDetailedInfo.createDate
    end,

    getModTypeText = function(self)
        return self.prosDetailedInfo.type
    end,

    getModCategoryText = function(self)
        return self.prosDetailedInfo.category
    end,

    getNumberOfViewsText = function(self)
        return self.prosDetailedInfo.views
    end,

    getModSizeText = function(self)
        return self.prosDetailedInfo.downloadSize
    end,

    getModDetailedDescriptionText = function(self)
        return self.prosDetailedInfo.longDesc
    end,

    getModStatusImage = function(self)
        if not self.waitingForDownloadFinished and self:getIsSubscribed() then
            return "common/img_542_1284.png"
        elseif self.waitingForDownloadFinished and self.subscriptionRequested then
            return "common/img_542_1288.png"
        else
            return "common/img_542_1280.png"
        end
    end,

    onLikeClicked = function(self)
    end,

    onSubscribeClicked = function(self)
        if not self:getIsSubscribed() then
            self:requestSubscribe()
        else
            self:requestUnsubscribe()
        end
    end,

    onDraw = function(self)
        local time = GetTime()
        local wasWaiting = self.waitingForDownloadFinished
        self.waitingForDownloadFinished = time < (self.startTime + self.timeToWait)

        if wasWaiting and not self.waitingForDownloadFinished and self.subscriptionRequested then
            self.subscriptionRequested = false
            self.isSubscribed = true
        end

        if self:getIsSubscribed() then
            self.subscribeButton.text = "Unsubscribe"
        elseif self.waitingForDownloadFinished then
            self.subscribeButton.text = "Downloading..."
        else
            self.subscribeButton.text = "Subscribe"
        end
    end,

    requestSubscribe = function(self)
        UiSubscribeToProsMod(self.prosShortInfo.id)
        self.startTime = GetTime()
        self.subscriptionRequested = true
        self.waitingForDownloadFinished = true
    end,

    requestUnsubscribe = function(self)
        UiUnsubscribeFromProsMod(self.prosShortInfo.id)
        self.isSubscribed = false
    end
}

ModBrowser.init  = function(self)

end


ModBrowser.currentModInfo = ModPreviewItemState:new()
