local AccountManager = class("AccountManager", BaseModule)

function AccountManager:showBindUI()
    UIManager:showUI("app/ui/game_setting/account_binding_ui")
end

function AccountManager:showDeleteUI(hideCloseBtn)
    return UIManager:showUI("app/ui/game_setting/account_delete_ui", {hideCloseBtn = hideCloseBtn})
end

function AccountManager:getIsBinded()
	local accountInfo = LocalData:getAccountInfo()
	if accountInfo.google_id and accountInfo.google_id ~= "" then
		return true
	end
	if accountInfo.apple_id and accountInfo.apple_id ~= "" then
		return true
	end
	return false
end

function AccountManager:deleteAccount()
	self:sendMessage(ProtoMsgType.FromMsgEnum.DeleteReq, {}, {}, self.onDeleteAccount, BIReport.ITEM_GET_TYPE.NONE)
end

function AccountManager:onDeleteAccount(result)
	if result.err_code == GConst.ERROR_STR.SUCCESS then -- 删除账号成功
		local info = LocalData:getLastLoginInfo()
		BIReport:postAccountDelete(info.type)
		ModuleManager.LoginManager:goToLoginScene()
		LocalData:saveBattleSnapshot({}) -- 清除
	else
		GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DELETE_ACCOUNT_FAILED))
    end
end

function AccountManager:bindAccount()
	local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
	local loginTypeStr = NetManager.LOGIN_TYPE.GOOGLE
    if Platform:isIosPlatform() then
		loginType = SDKManager.BF_LOGIN_TYPE.APPLE
		loginTypeStr = NetManager.LOGIN_TYPE.APPLE
    end

    BIReport:postAccountBindClick(loginTypeStr)
	SDKManager:login(function(params)
		if not params.token then
			return
		end
		local args = {
			type  = SDKManager.LOGIN_TYPE[loginType],
			id    = params.id,
			token = params.token
		}
		self:sendMessage(ProtoMsgType.FromMsgEnum.BindReq, args, {}, self.onBindAccount, BIReport.ITEM_GET_TYPE.NONE)
	end, loginType)
end

function AccountManager:onBindAccount(result)
	if result.status == 0 then
		local accountInfo = LocalData:getAccountInfo()
		local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
		local loginTypeStr = NetManager.LOGIN_TYPE.GOOGLE
		if Platform:isIosPlatform() then
			loginType = SDKManager.BF_LOGIN_TYPE.APPLE
			loginTypeStr = NetManager.LOGIN_TYPE.APPLE
			accountInfo.apple_id = result.reqData.id
		else
			accountInfo.google_id = result.reqData.id
		end
		LocalData:setLastLoginType(loginTypeStr)
		LocalData:setAccountInfo(accountInfo)
		LocalData:save()

		BIReport:postAccountBindFinish(loginTypeStr, true)
		GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.BIND_ACCOUNT_SUCCESS))
		EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.BIND_ACCOUNT_SUCCESS)
	else
		local params = {
			boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
			okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
	}
		if result.status == 13 then -- 重复绑定
			params.content = I18N:getGlobalText(I18N.GlobalConst.ALREADY_BINDED_DESC)
		else
			params.content = I18N:getGlobalText(I18N.GlobalConst.BIND_ACCOUNT_FAILED)
		end
		GFunc.showMessageBox(params)
		local loginTypeStr = NetManager.LOGIN_TYPE.GOOGLE
		if Platform:isIosPlatform() then
			loginTypeStr = NetManager.LOGIN_TYPE.APPLE
		end
		BIReport:postAccountBindFinish(loginTypeStr, false)
	end
end

function AccountManager:changeAccount()
	local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
	local loginTypeStr = NetManager.LOGIN_TYPE.GOOGLE
    if Platform:isIosPlatform() then
		loginType = SDKManager.BF_LOGIN_TYPE.APPLE
		loginTypeStr = NetManager.LOGIN_TYPE.APPLE
    end

    BIReport:postAccountChangeClick(loginTypeStr)
	SDKManager:login(function(params)
		if not params.token then
			return
		end
		local accountInfo = LocalData:getAccountInfo()
		local bindId = nil
		if Platform:isIosPlatform() then
			bindId = accountInfo.apple_id
		else
			bindId = accountInfo.google_id
		end
		if bindId and bindId ~= "" and bindId == params.id then
			local params = {
				content = I18N:getGlobalText(I18N.GlobalConst.CHANGE_ACCOUNT_FAILED_2),
				boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
				okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
			}
			GFunc.showMessageBox(params)
			return
		end
		local args = {
			type  = SDKManager.LOGIN_TYPE[loginType],
			id    = params.id,
			token = params.token
		}
		self:sendMessage(ProtoMsgType.FromMsgEnum.ExistReq, args, {}, self.onChangeAccount, BIReport.ITEM_GET_TYPE.NONE)
	end, loginType)
end

function AccountManager:onChangeAccount(result)
	if result.status == 0 then
		local accountInfo = LocalData:getAccountInfo()
		local loginType = SDKManager.BF_LOGIN_TYPE.GOOGLE
		local loginTypeStr = NetManager.LOGIN_TYPE.GOOGLE
		if Platform:isIosPlatform() then
			loginType = SDKManager.BF_LOGIN_TYPE.APPLE
			loginTypeStr = NetManager.LOGIN_TYPE.APPLE
			accountInfo.apple_id = result.reqData.id
		else
			accountInfo.google_id = result.reqData.id
		end
		LocalData:setLastLoginInfo(loginTypeStr, result.reqData.id, result.reqData.token)
		LocalData:setAccountInfo(accountInfo)
		LocalData:save()
		BIReport:postAccountChangeFinish(loginTypeStr, true)
		ModuleManager.LoginManager:goToLoginScene()
	else
		local params = {
			boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
			okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
		}
		if result.status == 14 then
			params.content = I18N:getGlobalText(I18N.GlobalConst.ACCOUNT_EXCHANGE_DESC)
		else
			params.content = I18N:getGlobalText(I18N.GlobalConst.CHANGE_ACCOUNT_FAILED)
		end
		GFunc.showMessageBox(params)
		local loginTypeStr = NetManager.LOGIN_TYPE.GOOGLE
		if Platform:isIosPlatform() then
			loginTypeStr = NetManager.LOGIN_TYPE.APPLE
		end
		BIReport:postAccountChangeFinish(loginTypeStr, false)
	end
end

return AccountManager