%% ---------------------------------------------------------------------------
%% @doc 场景玩家
%% @author hek
%% @since  2016-09-22
%% @deprecated 本模块更新场景玩家属性
%% ---------------------------------------------------------------------------
-module(lib_scene_user).
-export([set_data_sub/3]).
-include("scene.hrl").
-include("attr.hrl").
-include("figure.hrl").
-include("common.hrl").
-include("team.hrl").
-include("dungeon_multi.hrl").

-define(object_trace(X), {object_trace, X}).
%% 设置场景玩家属性(按属性列表更新)
%% @param AttrKeyValueList 属性列表 [{Key,Value}] Key为原子类型，Value为所需参数数据
%% @param SceneUser     当前状态
%% @return NewSceneUser 新状态
set_data_sub([], SceneUser, _MonMgrPid) -> SceneUser;
set_data_sub([{Key,Value}|T], SceneUser, MonMgrPid)->
    NewSceneUser =
        case Key of
            lv ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{lv = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            name ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{name = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            designation ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{designation = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            battle_attr ->
                %% 血量回复能力变化时，不更新定时器，等回血满了后续再变化
                %% 取消定时器再做更新管理开销稍大，没有太大必要
                SceneUser#ets_scene_user{battle_attr = Value};
            del_hp_each_time ->
                BA = SceneUser#ets_scene_user.battle_attr,
                SceneUser#ets_scene_user{battle_attr = BA#battle_attr{del_hp_each_time = Value}};
            att_del_hp_each_object ->
                BA = SceneUser#ets_scene_user.battle_attr,
                SceneUser#ets_scene_user{battle_attr = BA#battle_attr{att_del_hp_each_object = Value}};
            att_del_hp_each_time ->
                BA = SceneUser#ets_scene_user.battle_attr,
                SceneUser#ets_scene_user{battle_attr = BA#battle_attr{att_del_hp_each_time = Value}};
            speed ->
                BA = SceneUser#ets_scene_user.battle_attr,
                SceneUser#ets_scene_user{battle_attr = BA#battle_attr{speed = Value}};
            picture ->
                {Picture, PictureVer} = Value,
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{picture = Picture, picture_ver = PictureVer},
                SceneUser#ets_scene_user{figure = NewFigure};
            guild ->
                {GuildId, GuildName, Position, PositionName} = Value,
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{
                            guild_id = GuildId, guild_name = GuildName, position = Position,
                            position_name = PositionName},
                SceneUser#ets_scene_user{figure = NewFigure};
            team_flag ->
                #ets_scene_user{id = RoleId, copy_id = CopyId, x = TX, y = TY, team = OldTeam} = SceneUser,
                case OldTeam#status_team.team_id =/= Value#status_team.team_id
                    orelse OldTeam#status_team.leader_name =/= Value#status_team.leader_name of
                    true  ->
                        {X, Y} = lib_scene_calc:pixel_to_logic_coordinate(TX, TY),
                        Msg = {'team_flag', RoleId, Value#status_team.team_id, Value#status_team.leader_name},
                        [
                            % old: Aid ! {'team_flag', RoleId, Value#status_team.team_id, Value#status_team.leader_name}
                            mon_mgr:send(MonMgrPid, MonId, Msg)
                            || MonId <- lib_scene_object_agent:get_trace(CopyId, X, Y)
                        ];
                    false ->
                        ok
                end,
                SceneUser#ets_scene_user{team = Value};
            pk ->
                BA = SceneUser#ets_scene_user.battle_attr,
                SceneUser#ets_scene_user{battle_attr = BA#battle_attr{pk=Value}};
            pk_protect_time ->
                BA = SceneUser#ets_scene_user.battle_attr,
                OldPk = BA#battle_attr.pk,
                NewPk = OldPk#pk{pk_protect_time = Value},
                NewBA = BA#battle_attr{pk = NewPk},
                SceneUser#ets_scene_user{battle_attr = NewBA};
            is_ride ->
                SceneUser#ets_scene_user{is_ride = Value};
            mount_figure ->
                SceneUser#ets_scene_user{mount_figure = Value};
            wing_figure ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{wing_figure = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            fairy_figure ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{fairy_figure = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            cgw_king->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{king = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            fashion_model ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{fashion_model = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            light_weapon_model ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{light_weapon_model = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            role_foster ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure0 = Figure#figure{role_foster = Value},
                NewFigure  = lib_role_foster:update_lv_model(NewFigure0),
                SceneUser#ets_scene_user{figure = NewFigure};
            unite_buffs_cost->
                NValue = custom_unite_buff:scene_cost(SceneUser#ets_scene_user.unite_buffs,Value),
                SceneUser#ets_scene_user{unite_buffs = NValue};
            role_galaxy->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = lib_role_galaxy:update_galaxy_figure(Figure,Value),
                SceneUser#ets_scene_user{figure = NewFigure};
            visus ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{visus = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            foster_ride->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{is_ride = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            foster_skin ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{foster_skin = Value},
                % NewFigure  = lib_role_foster:update_lv_model(NewFigure0),
                SceneUser#ets_scene_user{figure = NewFigure};
            foster_sub_skin ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{foster_sub_skin = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            follow_pet ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{follow_pet = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            follow_child ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{baby_if_show = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            child_vehicle ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{baby_vehicle = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            child_fashion ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{baby_model = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            role_nucleon ->
                #ets_scene_user{figure = Figure} = SceneUser,
                SceneUser#ets_scene_user{figure = Figure#figure{nucleon_lv=Value}};
            show ->
                SceneUser#ets_scene_user{show = Value};
            weapon_effect ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{weapon_effect = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            vip ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{vip = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            follow_target_xy ->
                SceneUser#ets_scene_user{follow_target_xy=Value};
            lv_model ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{lv_model = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            liveness ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{liveness = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            turn ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{turn = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            passive_skill ->
                F = fun({SkillId, SLv}, TSkills) -> lists:keystore(SkillId, 1, TSkills, {SkillId, SLv}) end,
                NewSkillPassive = lists:foldl(F, SceneUser#ets_scene_user.skill_passive, Value ),
                SceneUser#ets_scene_user{skill_passive = NewSkillPassive};
            %% pet_passive_skill ->
            %%     F = fun({SkillId, SLv}, TSkills) -> lists:keystore(SkillId, 1, TSkills, {SkillId, SLv}) end,
            %%     NewSkillPassive = lists:foldl(F, SceneUser#ets_scene_user.pet_passive_skill, Value ),
            %%     SceneUser#ets_scene_user{pet_passive_skill = NewSkillPassive};
            delete_passive_skill ->
                F = fun({SkillId, _SLv}, TSkills) -> lists:keydelete(SkillId, 1, TSkills) end,
                NewSkillPassive = lists:foldl(F, SceneUser#ets_scene_user.skill_passive, Value),
                SceneUser#ets_scene_user{skill_passive = NewSkillPassive};
            quickbar_skills ->
                QuickbarSkill = quickbar_api:quickbar_skills(Value),
                SceneUser#ets_scene_user{quickbar_skills = QuickbarSkill};
            boss_tired ->
                SceneUser#ets_scene_user{boss_tired = Value};
            deserted_boss_tired ->
                SceneUser#ets_scene_user{deserted_boss_tired = Value};
            bl_who ->
                SceneUser#ets_scene_user{bl_who = Value};
            baby_battle_attr ->
                SceneUser#ets_scene_user{baby_battle_attr = Value};
            child_battle_attr ->
                SceneUser#ets_scene_user{child_battle_attr = Value};
            partner_battle_attr ->
                SceneUser#ets_scene_user{partner_battle_attr = Value};
            husong_angel_id ->
                Figure = SceneUser#ets_scene_user.figure,
                NewFigure = Figure#figure{husong_angel_id = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            mate_role_id ->
                #ets_scene_user{id = Id, x = X, y = Y, copy_id = CopyId} = SceneUser,
                {ok, BinData} = pt_120:write(12076, [Id, Value]),
                case lib_scene:is_broadcast_scene(SceneUser#ets_scene_user.scene) of
                    true ->
                        lib_scene_agent:send_to_local_scene(CopyId, BinData);
                    _ ->
                        lib_scene_agent:send_to_local_area_scene(CopyId, X, Y, BinData)
                end,
                SceneUser#ets_scene_user{mate_role_id = Value};
            figure ->
                SceneUser#ets_scene_user{figure = Value};
            att_tired ->
                SceneUser#ets_scene_user{att_tired = Value};
            skill_talent_active_list ->
                SceneUser#ets_scene_user{skill_talent_active_list = Value};
            act_label ->
                OActLabel = SceneUser#ets_scene_user.act_label,
                F = fun({K,V}, List) when V == 0-> lists:keydelete(K, 1, List);
                ({K,V}, List) -> [{K,V}|lists:keydelete(K, 1, List)]
                end,
                ActLabel = lists:foldl(F, OActLabel, Value),
                SceneUser#ets_scene_user{act_label = ActLabel};
            scene_att_objects ->
                SceneUser#ets_scene_user{scene_att_objects = Value};
            boss_crush_r ->
                SceneUser#ets_scene_user{boss_crush_r = Value};
            dun_multi ->
                case Value of
                    {TeamId, DunId} ->
                      SceneUser#ets_scene_user{dun_multi = #role_dun_multi{team_id = TeamId, dun_id = DunId}};
                    _ -> SceneUser
                end;
            can_att_list ->
                SceneUser#ets_scene_user{can_att_list = Value};
            cannot_beatt_list ->
                SceneUser#ets_scene_user{cannot_beatt_list = Value};
            collect_pid ->
                SceneUser#ets_scene_user{collect_pid = Value};
            mate_name ->
                SceneUser#ets_scene_user{mate_name = Value};
            mate_title ->
                SceneUser#ets_scene_user{mate_title = Value};
                mate_lv ->
                    SceneUser#ets_scene_user{mate_lv = Value};
            support_id ->
                SceneUser#ets_scene_user{support_id = Value};
            support_name ->
                SceneUser#ets_scene_user{support_name = Value};
            support_team ->
                SceneUser#ets_scene_user{support_team = Value};
            support_team_name ->
                SceneUser#ets_scene_user{support_team_name = Value};
            support_content ->
                SceneUser#ets_scene_user{support_content = Value};
            badge ->
                SceneUser#ets_scene_user{badge = Value};
            %% 正式代码加载上面：测试cd
            nocd ->
                if
                    Value == 1-> put({nocd, SceneUser#ets_scene_user.id}, 1);
                    true -> erase({nocd, SceneUser#ets_scene_user.id})
                end,
                SceneUser;
            anger ->
                case is_integer(Value) of
                    true  ->
                        #ets_scene_user{battle_attr = BattleAttr} = SceneUser,
                        SceneUser#ets_scene_user{battle_attr = BattleAttr#battle_attr{anger = Value}};
                    false ->
                        SceneUser
                end;
            wear_fashion ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{wear_fashion = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            follow_guard ->
                #ets_scene_user{figure = Figure} = SceneUser,
                NewFigure = Figure#figure{follow_guard = Value},
                SceneUser#ets_scene_user{figure = NewFigure};
            support_pet_list ->
                SceneUser#ets_scene_user{support_pet_list = Value};
            reb_protect_time ->
                SceneUser#ets_scene_user{reb_protect_time = Value};
            deb_protect_time ->
                SceneUser#ets_scene_user{deb_protect_time = Value};
        _ ->
            SceneUser
    end,
    set_data_sub(T, NewSceneUser, MonMgrPid).
