--[[
 -- @Description:
 -- Hello , DiaoSi ~ ( or not ? ) ,glad you saw my comment
 -- If you think my mod is okay , you can sponsor my breakfast by a tiny amount donation  ($_$)
 -- paypal:td1madao@163.com     alipay:td1madao@163.com
 -- If any question ,Please email me
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- What's more ,don't modify and republish my mods . 3Q for cooperation
 -- @version 1.0.0
 -- @author td1madao
 -- @email td1madao@163.com
 -- @qq 360810498
 -- @date 16/8/6
 ]]
local TD = TD.td1madao.rivendst
local Rivendst_common_synchronizer = Class(function(self, inst)
    TD.see()
    self.inst = inst
    TD.common(inst, function()
        local skill_manager = TD.getC(inst, "rivendst_common_skill_manager")
        local skill = skill_manager.skill
        local skillBuffer = skill_manager.skillBuffer
        self.rivenRtwice = net_bool(inst.GUID, "td1madao.rivendst.rivenRtwice", "td1madao_rivendst_rivenRtwice")
        self.level = net_ushortint(inst.GUID, "td1madao.rivendst.level", "td1madao_rivendst_level")
        self.exp = net_uint(inst.GUID, "td1madao.rivendst.exp", "td1madao_rivendst_exp")
        self.all_in_cd = net_smallbyte(inst.GUID, "td1madao.rivendst.all_in_cd", "td1madao_rivendst_all_in_cd")
        TD.latter(self.inst, 1, function()
            if TD.isPlayer(self.inst) then
                if self.level:value() == 0 then
                    self.level:set(1)
                end
                self.inst:ListenForEvent("td1madao_rivendst_level", function()
                    self:player_fresh_att()
                end)
                self.inst:ListenForEvent("td1madao_rivendst_exp", function()
                    self:player_fresh_att()
                end)
                self.inst:ListenForEvent("td1madao_rivendst_all_in_cd", function()
                    local num = self:player_get("all_in_cd")
                    local skillLen = table.td1madao_entLen(skill)
                    num = math.mod(num, skillLen)
                    if num == 0 then
                        num = skillLen
                    end
                    local tag = skill_manager:server_num2letter(num)
                    if tag ~= "nil" then
                        skill[tag].cd = skillBuffer[tag].maxCd
                        TD.getC(self.inst, "rivendst_common_skill_manager"):player_showAsCd(tag)
                    end
                end)
                self:player_fresh_att()
            end
        end)
        TD.server(function()
            self.server_qtime = 0
            self.server_x = 0
            self.server_y = 0
            self.server_z = 0
            self.server_dmg = 10
            self.server_q3fly = false
            self.server_local_tag = "nil"
            self.server_aftercast = false
            self.server_damagemultiplierDefault = 1
            self.server_armor = 0
            self.server_rivenRBuff = false
            self.server_angel = 0
        end)
    end)
end)


function Rivendst_common_synchronizer:player_fresh_att()
    local player = self.inst
    local td1madaoRivendstSkillControls = player.td1madaoRivendstSkillControls
    td1madaoRivendstSkillControls.level:SetString(string.format("lv:%d", self:player_get("level") or 1))
    td1madaoRivendstSkillControls.exp:SetString(string.format("exp:%s/%s", self:player_get("exp") or 0, self:player_getExpBase()))
end


function Rivendst_common_synchronizer:player_getExpBase()
    local level = self:player_get("level")
    local need = math.ceil(math.pow(MADAO.RIVENDST_EXP_RAISE, (level - 1)) * MADAO.RIVENDST_EXP_BASE)
    return need
end

function Rivendst_common_synchronizer:server_setAllInCd(val)
    TD.see()
    local name = "all_in_cd"
    if name and val ~= nil and self[name] then
        local oldNum = self[name]:value()
        if val == oldNum then
            local skill = TD.getC(self.inst, "rivendst_common_skill_manager").skill
            val = val + table.td1madao_entLen(skill)
        end
        self[name]:set(val)
    end
end

function Rivendst_common_synchronizer:server_set(name, val, force)
    TD.see()
    if name and val ~= nil and self[name] then
        if force then
            self[name]:set(val)
        else
            local lo = self:server_get(name)
            if lo ~= val then
                self[name]:set(val)
            end
        end
    end
end

function Rivendst_common_synchronizer:common_get(name)
    -- TD.see()
    if name and self[name] then
        return self[name]:value()
    end
    return false
end



function Rivendst_common_synchronizer:player_get(name)
    -- TD.see()
    return self:common_get(name)
end

function Rivendst_common_synchronizer:server_get(name)
    -- TD.see()
    return self:common_get(name)
end

return Rivendst_common_synchronizer