local RunesManager = class("RunesManager", BaseModule)

-- 当前是否处于自动淬炼中
function RunesManager:isInAutoQuenching()
    return self.isAutoQuenching == true
end

-- 是否处于正在同步自动淬炼次数中
function RunesManager:isInSyncQuenching()
    return self.isSyncQuenching == true
end

-- 锁定和解锁属性栏
function RunesManager:reqChangeLockGrid(heroId, index, isLock)
    if self.isReqLockChange then
        return
    end
    self.isReqLockChange = true

    self:sendMessage(ProtoMsgType.FromMsgEnum.RuneLockReq, {hero_id = heroId, grid_num = index, lock = isLock}, {}, self.rspChangeLockGrid)
end

function RunesManager:rspChangeLockGrid(result)
    self.isReqLockChange = false
    if result.hero_girds then
        DataManager.RunesData:onGridLockSuccess(result.reqData.hero_id, result.hero_girds.grids)
    end
end

-- 淬炼
function RunesManager:reqQuenching(heroId)
    self:sendMessage(ProtoMsgType.FromMsgEnum.RuneQuenchingReq, {hero_id = heroId}, {}, self.rspQuenching, BIReport.ITEM_GET_TYPE.RUNES_QUENCHING)
end

function RunesManager:rspQuenching(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.RunesData:onQuenchingSuccess(result.level, result.exp, result.reqData.hero_id, result.hero_girds.grids)

        if result.auto_count and result.auto_count > 0 then
            ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_RUNES_QUENCHING, result.auto_count)
        else
            ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_RUNES_QUENCHING, 1)
        end
    end
end

-- 获取自动淬炼数据
function RunesManager:reqAutoQuenching(heroId)
    self:sendMessage(ProtoMsgType.FromMsgEnum.RuneQuenchingAutoReq, {hero_id = heroId}, {}, self.rspAutoQuenching)
end

function RunesManager:rspAutoQuenching(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        self.isAutoQuenching = true
        DataManager.RunesData:onGetAutoQuenchingDataSuccess(result.reqData.hero_id, result.count, result.stop_with_s)
    end
end

-- 同步淬炼结果，共有四种情况
--1、玩家手动停止（停止）
--2、材料不够（停止）
--3、自动次数用完（同步已执行次数，继续请求自动数据）
--4、同步（仅同步已执行次数）
function RunesManager:reqSyncQuenching(isEnd, onlySyncTime)
    if self.isSyncQuenching then
        return
    end
    self.isSyncQuenching = true
    self:sendMessage(ProtoMsgType.FromMsgEnum.RuneQuenchingAutoSyncReq, {count = DataManager.RunesData:getExecutedAutoCount(), isEnd = isEnd, onlySyncTime = onlySyncTime}, {}, self.rspSyncQuenching, BIReport.ITEM_GET_TYPE.RUNES_AUTO_QUENCHING, not onlySyncTime)
end

function RunesManager:rspSyncQuenching(result)
    self.isSyncQuenching = false
    if result.reqData.isEnd then
        -- 停止
        self.isAutoQuenching = false
    end
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        if not result.reqData.isEnd and not result.reqData.onlySyncTime then
            -- 同步已执行次数，继续请求自动数据
            ModuleManager.RunesManager:reqAutoQuenching(DataManager.RunesData:getAutoHeroId())
        end
        DataManager.RunesData:onAutoQuenchingSuccess(result.level, result.exp, result.hero_girds.grids)
    else
        DataManager.RunesData:onAutoQuenchingFailed()
    end
end

function RunesManager:rspUpdate(result)
    DataManager.RunesData:init(result.rune)
end

return RunesManager