%% ---------------------------------------------------------------------------
%% @doc lib_role.erl

%% @author  hjh
%% @email  hejiahua@163.com
%% @since  2016-12-26
%% @deprecated 角色辅助信息
%% ---------------------------------------------------------------------------
-module(lib_role).
-export([]).

-compile(export_all).

-include("figure.hrl").
-include("role.hrl").
-include("title.hrl").
-include("predefine.hrl").
-include("server.hrl").
-include("guild.hrl").
-include("vip.hrl").
-include("common.hrl").
-include("transform.hrl").
-include("def_fun.hrl").
-include("rec_event.hrl").
-include("rec_dress_up.hrl").

%% ----------------------------------------------------
%% #ets_role_show{}
%% ----------------------------------------------------

login(PS) ->
    case ets:lookup(?ETS_ROLE_SHOW, PS#player_status.id) of
        [] ->
            #player_status{
                id = Id, accid = AccId, accname = AccName,figure = Figure, last_login_time = LastLoginTime,
                last_logout_time = LastLogoutTime, combat_power = CombatPower,
                hightest_combat_power = HCombatPower
            } = PS,
            RoleShow = #ets_role_show{
                id = Id, accid = AccId, accname = AccName, figure = Figure, online_flag = ?ONLINE_ON
                , last_login_time = LastLoginTime, last_logout_time = LastLogoutTime
                , combat_power = CombatPower, h_combat_power = HCombatPower
                , mate_title = mate_api:role_get_mate_title(PS)
            },
            ets:insert(?ETS_ROLE_SHOW, RoleShow),
            ok;
        _ -> skip
    end.

%% 获得玩家的展示
get_role_show(0) -> [];
get_role_show(Id) ->
    case ets:lookup(?ETS_ROLE_SHOW, Id) of
        [] ->
            case get_figure_on_db(Id) of
                [] -> [];
                Figure ->
                    [AccId, AccName, OnlineFlag, LastLoginTime, LastLogoutTime, LastLogoutCombatPower, HCombatPower] = get_role_show_other_info(Id),
                    RealOnlineFlag = ?IF(OnlineFlag == ?ONLINE_ON, ?ONLINE_ON, ?ONLINE_OFF),
                    RoleShow = #ets_role_show{
                        id = Id
                        , accid = AccId
                        , accname = AccName
                        , figure = Figure#figure{power = LastLogoutCombatPower}
                        , online_flag = RealOnlineFlag
                        , last_login_time = LastLoginTime
                        , last_logout_time = LastLogoutTime
                        , combat_power = LastLogoutCombatPower
                        , h_combat_power = HCombatPower
                        , mate_title = mate_api:get_role_mate_title(Id)
                        },
                    ets:insert(?ETS_ROLE_SHOW, RoleShow),
                    RoleShow
            end;
        [RoleShow] ->
            RoleShow
    end.

get_role_name(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{figure = Figure} ->
            Figure#figure.name;
        _ -> <<>>
    end.

get_role_vip(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{figure = Figure} ->
            {Figure#figure.vip_type, Figure#figure.vip};
        _ -> {0, 0}
    end.

get_role_lv(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{figure = Figure} ->
            Figure#figure.lv;
        _ -> 0
    end.

get_role_gender(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{figure = Figure} ->
            Figure#figure.sex;
        _ ->
            0
    end.

get_role_combat_power(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{combat_power = CombatPower, h_combat_power = HCombatPower} ->
            case CombatPower > 0 of
                true -> CombatPower;
                false -> HCombatPower
            end;
        _ -> 0
    end.

get_role_figure(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{figure = Figure} -> Figure;
        _ -> #figure{}
    end.

get_role_logout_time(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{last_logout_time = Time} ->
            Time;
        _ ->
            0
    end.

get_role_elem(RoleId, Index, DefaultValue) ->
    case get_role_show(RoleId) of
        #ets_role_show{} = Role -> element(Index, Role);
        _ -> DefaultValue
    end.

get_role_guild(RoleId) ->
    case get_role_show(RoleId) of
        #ets_role_show{figure = #figure{guild_id = GuildId}} -> GuildId;
        _ -> 0
    end.

%% 更新玩家展示Ets
update_role_show(Id, KeyList) ->
    case ets:lookup(?ETS_ROLE_SHOW, Id) of
        [] -> skip;
        [RoleShow] ->
            NewRoleShow = do_update_role_show(KeyList, RoleShow),
            ets:insert(?ETS_ROLE_SHOW, NewRoleShow)
    end.

do_update_role_show([], RoleShow) -> RoleShow;
do_update_role_show([T|L], RoleShow) ->
    #ets_role_show{figure = Figure} = RoleShow,
    case T of
        {figure, NewFigure} -> NewRoleShow = RoleShow#ets_role_show{figure = NewFigure};
        {guild_id, GuildId} -> NewRoleShow = RoleShow#ets_role_show{ figure = Figure#figure{guild_id = GuildId} };
        {guild_name, GuildName} -> NewRoleShow = RoleShow#ets_role_show{ figure = Figure#figure{guild_name = GuildName} };
        {position, Position} -> NewRoleShow = RoleShow#ets_role_show{ figure = Figure#figure{position = Position} };
        {position_name, PositionName} -> NewRoleShow = RoleShow#ets_role_show{ figure = Figure#figure{position_name = PositionName} };
        {combat_power, CombatPower} ->
            NewRoleShow = RoleShow#ets_role_show{combat_power = CombatPower, figure = Figure#figure{power = CombatPower}};
        {h_combat_power, HCombatPower} -> NewRoleShow = RoleShow#ets_role_show{h_combat_power = HCombatPower};
        {last_logout_time, LastLogoutTime} ->
            NewRoleShow = RoleShow#ets_role_show{last_logout_time = LastLogoutTime};
        {last_login_time, LastLoginTime} ->
            NewRoleShow = RoleShow#ets_role_show{last_login_time = LastLoginTime};
        {online_flag, OnlineFlag} ->
            NewRoleShow = RoleShow#ets_role_show{online_flag = OnlineFlag};
        {left_activity_num_list, LeftActivityNumList} -> NewRoleShow = RoleShow#ets_role_show{left_activity_num_list = LeftActivityNumList};
        {dungeon_count_change, {ActivityKey, LeftCount} } ->
            LeftActivityNumList = RoleShow#ets_role_show.left_activity_num_list,
            NewLeftActivityNumList = lists:keystore(ActivityKey, 1, LeftActivityNumList, {ActivityKey, LeftCount}),
            NewRoleShow = RoleShow#ets_role_show{left_activity_num_list = NewLeftActivityNumList};
        {mate_title, {Other, Title} } ->
            MateTitle = RoleShow#ets_role_show.mate_title,
            NewMateTitle = lists:keystore(Other, 1, MateTitle, {Other, Title}),
            NewRoleShow = RoleShow#ets_role_show{mate_title = NewMateTitle};
        {fashion_model, FashionModels} ->
            NewRoleShow = RoleShow#ets_role_show{figure = Figure#figure{fashion_model = FashionModels}};
        {wear_fashion, Value} ->
            NewRoleShow = RoleShow#ets_role_show{figure = Figure#figure{wear_fashion = Value}};
        {designation, DsgtId} ->
            NewRoleShow = RoleShow#ets_role_show{figure = Figure#figure{designation = DsgtId}};
        {vip, VipLv} ->
            NewRoleShow = RoleShow#ets_role_show{figure = Figure#figure{vip = VipLv}};
        {sup_vip, SupVip} ->
            NewRoleShow = RoleShow#ets_role_show{figure = Figure#figure{sup_vip = SupVip}};
        {role_galaxy,Value}->
            NewFigure = lib_role_galaxy:update_galaxy_figure(Figure,Value),
            NewRoleShow = RoleShow#ets_role_show{figure = NewFigure};
        {follow_guard, Value} ->
            NewRoleShow = RoleShow#ets_role_show{figure = Figure#figure{follow_guard = Value}};
        {husong_angel_id, Value} ->
            NewRoleShow = RoleShow#ets_role_show{figure = Figure#figure{husong_angel_id = Value}};
        _ -> NewRoleShow = RoleShow
    end,
    do_update_role_show(L, NewRoleShow).

%% 获得figure
get_figure_on_db(Id) ->
    % TODO:优化sql
    case lib_player:get_player_login_data(Id) of
        [GM|_] ->
            [NickName, Sex, Lv, Career, Realm, Picture, _PictureLim, PictureVer,ProfilePhotoId|_] = lib_player:get_player_low_data(Id),
            #transform_info{transform_id = Turn, stage = TurnStage} = transform:login(Id),
            LvModel = lib_goods_util:get_lv_model(Id, Career, Sex, Turn, Lv),
            {FashionModelList, WearFashion} = lib_fashion:get_equip_fashion_list(Id, Career),
            StatusGuild = lib_guild:load_status_guild(Id,utime:unixtime(),0),
            VipLv = lib_vip_util:get_vip_lv_db(Id),
            SupVipType = lib_sup_vip:get_sup_vip_on_db(Id),
            DsgtId = lib_designation:db_get_designation(Id),
            {_, _, DressList} = lib_dress_up:load_dress_list(Id),
            FollowPet = lib_pet_api:db_get_follow_pet(Id),
            Figure = #figure{id = Id, name=NickName, sex=Sex, lv=Lv, career=Career, realm=Realm, gm=GM, vip = VipLv,
                    lv_model = LvModel, fashion_model = FashionModelList, picture = Picture, picture_ver = PictureVer,
                    profile_photo_id = ProfilePhotoId, wear_fashion = WearFashion,
                    guild_id = StatusGuild#status_guild.id,
                    guild_name = StatusGuild#status_guild.name,
                    position = StatusGuild#status_guild.position,
                    position_name = StatusGuild#status_guild.position_name,
                    designation = DsgtId, dress_list = DressList,
                    turn = Turn, sup_vip = SupVipType, follow_pet = FollowPet,
                    turn_stage = TurnStage
                   },
            Figure0 = lib_role_foster:db_load(Id,Figure),
            NewFigure = lib_role_foster_skin:db_load(Id,Figure0),
            NewFigure;
        [] ->
            []
    end.

%% 获得基础的base(只有头像基础信息,去掉多余的sql)
get_base_figure_on_db(Id) ->
    case lib_player:get_player_login_data(Id) of
        [GM|_] ->
            [NickName, Sex, Lv, Career, Realm, Picture, _PictureLim, PictureVer,ProfilePhotoId|_] = lib_player:get_player_low_data(Id),
            %% #status_title{id = TitleId} = lib_title:get_title(Id),
            VipLv = lib_vip_util:get_vip_lv_db(Id),
            #figure{id = Id, name=NickName, sex=Sex, lv=Lv, career=Career, realm=Realm, gm=GM, vip = VipLv,
                    picture = Picture, picture_ver = PictureVer,profile_photo_id = ProfilePhotoId};
        [] ->
            []
    end.

%% 获取展示的其他信息
%% @param [OnlineFlag, LastLoginTime, HCombatPower]
get_role_show_other_info(RoleId) ->
    Sql = io_lib:format(?sql_role_show_other_info, [RoleId]),
    db:get_row(Sql).

%% 获取最后退出游戏时候玩家战力
get_role_last_logout_power(RoleId) ->
    Sql = io_lib:format(?sql_role_last_logout_power, [RoleId]),
    db:get_row(Sql).

figure_to_rec_picture(Figure = #figure{}) ->
    PictureFrame =
    case lists:keyfind(?DRESS_UP_FRAME, 1, Figure#figure.dress_list) of
        {_DType, DId}  -> DId;
        _ -> 0
    end,
    #rec_picture{
      id = Figure#figure.id,
      name= Figure#figure.name,
      sex = Figure#figure.sex,
      career = Figure#figure.career,
      vip = Figure#figure.vip,
      sup_vip = Figure#figure.sup_vip,
      turn = Figure#figure.turn,
      lv = Figure#figure.lv,
      power = Figure#figure.power,
      picture_frame = PictureFrame,
      picture = Figure#figure.picture,
      picture_ver = Figure#figure.picture_ver,
      profile_photo_id = Figure#figure.profile_photo_id
    };
figure_to_rec_picture(_) ->
    #rec_picture{}.

%% ----------------------------------------------------
%% @doc 查看角色信息
%% ----------------------------------------------------

