%% ---------------------------------------------------------------------------
%% @doc 个性装扮协议文件
%% @author tyl
%% @since 2019-11-07
%% @end
%% ---------------------------------------------------------------------------
-module(pp_dress_up).
-include("server.hrl").
-include("common.hrl").
-include("figure.hrl").
-include("errcode.hrl").
-include("rec_dress_up.hrl").
-include("def_fun.hrl").
-include("predefine.hrl").
-include("attr.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("goods.hrl").
-include("def_event.hrl").

-export([handle/3]).

%% 获取装扮列表
handle(11200, PS, [DressType]) ->
    #player_status{dress_up = StatusDressUp, sid = Sid, figure = _Figure} = PS,
    #status_dress_up{enabled_map = EnabledMap, using_map = UsingMap} = StatusDressUp,
    AllDressType = case data_dress_up:get_all_dress_type() of
                       [_|_] = TypeList -> TypeList;
                       _ -> []
                   end,
    case lists:member(DressType, AllDressType) of
        false -> ok;
        _ ->
            EnabledList = maps:get(DressType, EnabledMap, []),
            case maps:get(DressType, UsingMap, []) of
                UsingDressId when is_integer(UsingDressId) ->
                    lib_server_send:send_to_sid(Sid, pt_112, 11200, [?SUCCESS, DressType, EnabledList, UsingDressId]),
                    {ok, PS};
                _ ->
                    %% 如果没有正在使用的装扮就激活默认的吧(激活默认失败也返回默认Id)
                    util:send_after([], 2*1000, self(), {'mod', lib_dress_up, active_default_dress, [DressType]}),
                    {ok, PS}
            end
    end;

%% 激活装扮
handle(11201, PS, [DressId]) ->
    #player_status{sid = Sid, dress_up = DressStatus, figure = Figure} = PS,
    OldEnabledMap = DressStatus#status_dress_up.enabled_map,
    case data_dress_up:get_dress_cfg(DressId) of
        #base_dress_up{
            dress_id          = DressId,
            dress_type        = DressType,
            dress_consume_id  = CostId
        } ->
            ActiveNum = 1, %% 激活所需的道具个数
            CheckList = [
                {has_enabled, OldEnabledMap, DressType, DressId},
                {goods_enough, CostId, ActiveNum}
                % {cost_goods, CostId, ActiveNum}
            ],
            case lib_dress_up:check_enable(PS, CheckList) of
                {true, _Ps} ->
                    CostList = [{CostId, 1}],
                    case lib_goods_api:cost_object_list(PS, CostList, acive_dress_up, "active dress_up") of
                        {true, NewPS} ->
                            OldDressList   = maps:get(DressType, OldEnabledMap, []),
                            NewDressList   = [DressId|OldDressList],
                            NewEnabledMap  = maps:put(DressType, NewDressList, OldEnabledMap),
                            StatusDressUp2 = DressStatus#status_dress_up{enabled_map = NewEnabledMap},
                            lib_dress_up:db_replace_dress_up_enabled(NewPS#player_status.id, DressId, DressType, ?DRESS_UNUSING),
                            SettedDressPS = NewPS#player_status{dress_up = StatusDressUp2},
                            {ColorList, NotOpenNum} = lib_dress_up:calc_all_dress_up(Figure#figure.lv, DressType, NewEnabledMap),
                            lib_dress_up:handle_achievement(SettedDressPS, NewEnabledMap),
                            {ok, SettedDressPS1} = lib_wardrobe_active:active_dress_up(SettedDressPS, DressType,
                                ColorList, NotOpenNum, DressId),
                            RefreshedPS = lib_dress_up:refresh_attr(SettedDressPS1, active_dress_up),
                            lib_server_send:send_to_sid(Sid, pt_112, 11201, [?SUCCESS, DressId, DressType]),
                            {ok, RefreshedPS};
                        {false, Err, _} ->
                            lib_server_send:send_to_sid(Sid, pt_112, 11201, [Err, DressId, DressType]),
                            {ok, PS}
                    end;
                {false, Res} ->
                    lib_server_send:send_to_sid(Sid, pt_112, 11201, [Res, DressId, DressType]),
                    {ok, PS}
            end;
        _ ->
            lib_server_send:send_to_sid(Sid, pt_112, 11201, [?ERRCODE(err112_dress_not_exist), DressId, ?DRESS_UP_BUBBLE]),
            {ok, PS}
    end;

%% 使用装扮  策划增加需求:同时保存多个类型装扮组合
%% update 191107
handle(11202, PS, [NeedDressList]) ->
    Len = length(NeedDressList),
    case Len > 0 of
        true ->
            %% 判断参数是否都正确
            CheckConfig = lib_dress_up:check_configs(NeedDressList, Len),
            %% 判断是否是同一类型装扮
            CheckSameType = lib_dress_up:check_same_type(NeedDressList),
            if
                not CheckConfig ->
                    lib_server_send:send_to_sid(PS#player_status.sid, pt_112, 11202, [?ERRCODE(err112_dress_not_exist), []]),
                    {ok, PS};
                not CheckSameType ->
                    lib_server_send:send_to_sid(PS#player_status.sid, pt_112, 11202, [?ERRCODE(err112_dress_same_type), []]),
                    {ok, PS};
                true ->
                    NewPlayer =
                    case lib_dress_up:save_dress_up_combination(NeedDressList, PS, []) of
                        {true, NewPs, DressList} ->
                            %% 写库
                            F = fun({{_DressType, DressId}, PreDressId}, Ack) ->
                                case DressId == PreDressId of
                                    true -> Ack;
                                    false ->
                                        lib_dress_up:db_update_dress_up_enabled(PS#player_status.id, PreDressId, ?DRESS_UNUSING),
                                        lib_dress_up:db_update_dress_up_enabled(PS#player_status.id, DressId, ?DRESS_ISUSING),
                                        Ack
                                end end,
                            F1 = fun() -> lists:foldl(F, true, DressList), true end,
                            case catch db:transaction(F1) of
                                true ->
                                    lib_role:update_role_show(NewPs#player_status.id, [{figure, NewPs#player_status.figure}]),
                                    NewDressList = [{TDressId, TDressType} || {{TDressType, TDressId}, _} <- DressList],
                                    lib_server_send:send_to_sid(NewPs#player_status.sid, pt_112, 11202, [?SUCCESS, NewDressList]),
                                    ChangeList = [{TDressId, TDressType} || {{TDressType, TDressId}, PDressId} <- DressList, PDressId =/= TDressId],
                                    {ok, NewPs1} = lib_player_event:dispatch(NewPs, ?EVENT_DRESS_UP_CHANGE, ChangeList),
                                    NewPs1;
                                _ ->
                                    lib_server_send:send_to_sid(PS#player_status.sid, pt_112, 11202, [?FAIL, []]),
                                    PS
                            end;
                        {false, Err, _NewPs} ->
                            lib_server_send:send_to_sid(PS#player_status.sid, pt_112, 11202, [Err, []]),
                            PS
                    end,
                    {ok, NewPlayer}
            end;
        _  ->
            lib_server_send:send_to_sid(PS#player_status.sid, pt_112, 11202, [?ERRCODE(err112_dress_not_selected), []]),
            {ok, PS}
    end;

%% 回收装扮 2020-03-31
handle(11203, PS, [DressType, DressId, GoodsId]) ->
    #player_status{sid = Sid, id = RoleId, dress_up = DressStatus} = PS,
    EnabledMap = DressStatus#status_dress_up.enabled_map,
    CheckList = [{has_active, EnabledMap, DressType, DressId}, {has_good, GoodsId}],
    case lib_dress_up:check_recycle(PS, CheckList) of
        {true, _Ps} ->
            case lib_goods_api:delete_one_by_id(PS, GoodsId, DressId, 1, dress_up_recycle) of
                1 ->
                    case data_goods_price:get_goods_price(DressId) of
                        #goods_price{price_type = PriceType, price = Price} ->
                            Title = utext:get(1120001), Content = utext:get(1120002),
                            lib_mail_api:send_sys_mail([RoleId], ?MOD_DRESS_UP, Title, Content, [{PriceType, 0, Price}]),
                            lib_server_send:send_to_sid(Sid, pt_112, 11203, [?SUCCESS]);
                        _ ->
                            lib_server_send:send_to_sid(Sid, pt_112, 11203, [?FAIL])
                    end;
                Err ->
                    lib_server_send:send_to_sid(Sid, pt_112, 11203, [Err])
            end;
        {false, Res} ->
            lib_server_send:send_to_sid(Sid, pt_112, 11208, [Res])
    end,
    {ok, PS};

handle(_Cmd, _PS, _Data) ->
    {error, "pp_dress_up2 no match"}.
