%% ---------------------------------------------------------------------------
%% @doc 模拟登陆数据加载-获取离线玩家数据
%% @author hek
%% @since  2016-11-30
%% @deprecated
%% ---------------------------------------------------------------------------
-module(lib_offline_login).
-include("server.hrl").
-include("figure.hrl").
-include("attr.hrl").
-include("predefine.hrl").
-include("rec_offline.hrl").
-include("title.hrl").
-include("guild.hrl").
-include("goods.hrl").
-include("def_goods.hrl").
-include("vip.hrl").
-include("team.hrl").
-include("transform.hrl").
-include("fashion.hrl").

-export ([load_player_info/2]).

%% 加载离线玩家数据
%% 注:主要加载以下数据，除此之外的数据一般不需要加载
%% 1.形象数据 请用lib_role
%% 2.13004查看离线玩家信息需要用到的数据
load_player_info(Id, NowTime) ->
    [_DbScene, _DbX, _DbY, _Hp, QuickbarBin, _PkValue, _PkStatus, _PkStatusChangeTime, SkillPoint,
        _PkValueChangeTime, _LastTaskId, _BitLastBeKill, LastCombatPower, PassiveSkillBin, AttrListBin|_]
        = lib_player:get_player_state_data(Id),
    [CellNum, StorageNum|_] = lib_player:get_player_attr_data(Id),
    #figure{lv = Lv, career = Career, sex = Sex, vip_type = VipType, vip = VipLv}
        = Figure = lib_role:get_role_figure(Id),
    %% 物品初始化
    GoodsStatus = login_goods(Id, Lv, CellNum, StorageNum, NowTime, Career),
    %% 临时存储物品进程数据，等计算完战力之后后清除
    lib_goods_do:set_goods_status(GoodsStatus),
    Off = #status_off{
        goods_status = GoodsStatus,
        relationship = lib_relationship:get_rela_from_db(Id)
    },
    %% 技能初始化
    Skill = lib_skill:skill_online(Id, Career, Lv, SkillPoint, {VipType, VipLv, 0}),
    PlayerStatus = #player_status{
            id              = Id,
            figure          = Figure,
            off             = Off,
            quickbar        = quickbar_api:load_check_quickbar(QuickbarBin, Career, Sex)
            ,mate           = mate_api:login(Id)
            ,cell_num       = CellNum
            ,storage_num    = StorageNum
            ,skill          = Skill
            ,accname = Figure#figure.accname
            ,role_dun_pet   = lib_dungeon_pet:login(Id)
    },
    %% 宠物
    PetPs = lib_pet_api:login(PlayerStatus),
    %% 孩子
    ChildPs = lib_child_api:login(PetPs),
    %%进阶系统
    FosterPs = lib_role_foster:login(ChildPs),
    %%进阶珍宝
    FosterSkinPs = lib_role_foster_skin:login(FosterPs),
    %% 灵能
    NucPs    = lib_role_nucleon:login(FosterSkinPs),
    %% 装备系统
    Ps1 = equipment_api:login(NucPs),
    %% 充值信息
    LastPs = lib_recharge:load_data(Ps1),
    %% 其实不需要进行属性计算：因为已经直接在数据库读取了战力和总属性
    %% 清除临时存储的物品进程数据
    erlang:erase({lib_goods_do, player_goods_status}),
    Attr = attr_conv:to_attr_record(util:bitstring_to_term(AttrListBin)),
    %% figure里战力用离线战力
    ThenFigure = LastPs#player_status.figure,
    LastPs#player_status{
        combat_power = LastCombatPower,
        battle_attr = #battle_attr{
            attr = Attr,
            hp = Attr#attr.hp,
            hp_lim = Attr#attr.hp,
            combat_power = LastCombatPower
        },
        figure = ThenFigure#figure{power = LastCombatPower},
        passive_skills = util:bitstring_to_term(PassiveSkillBin)
    }.

%% 物品加载
login_goods(Id, Lv, _CellNum, _StorageNum, NowTime, _Career) ->
    BagList = [
        ?GOODS_LOC_EQUIP
        ,?GOODS_LOC_ARMOR_EQUIP
        ,?GOODS_LOC_WAR_SOUL_EQUIP
    ],
    GoodsMaps = lib_goods_init:get_location_dict(BagList, Id, #{}, Lv, NowTime),
    NewGoodsStatus = #goods_status{
        player_id = Id,
        player_lv = Lv,
        dict = GoodsMaps
    },
    NewGoodsStatus.