%%%--------------------------------------
%%% @Module  : pp_fashion
%%% @Author  : huyihao
%%% @Created : 2017.10.26
%%% @Description:  时装
%%%--------------------------------------

-module(pp_fashion).

-export([handle/3]).
-include("server.hrl").
-include("goods.hrl").
-include("common.hrl").
-include("errcode.hrl").
-include("def_event.hrl").
-include("def_goods.hrl").
-include("fashion.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("common_rank.hrl").
-include("def_module.hrl").

-type(player() :: #player_status{}).
-spec(handle(integer(), player(), list())       -> {ok, player()} | any()).

%% ===============================================================================================
handle(Cmd, Ps, Args) ->
    #player_status{figure = Figure} = Ps,
    #figure{lv = Lv} = Figure,
    ConfigLv = lib_module:get_open_lv(?MOD_FASHION, 1),
    case Lv >= ConfigLv of
        true ->
            do_handle(Cmd, Ps, Args);
        false ->
            skip
    end.

%% 时装信息
do_handle(41300, Player, _) ->
    #player_status{sid = Sid, role_fashion = RoleFashion} = Player,
    #role_fashion{all_fashions = FashionPosList} = RoleFashion,
    FashionData = lib_fashion:pack_fashion_info(FashionPosList),
    {ok, BinData} = pt_413:write(41300, [?SUCCESS, FashionData]),
    lib_server_send:send_to_sid(Sid, BinData),
    ok;

%% 穿戴时装颜色
do_handle(41302, Player, [PosId, FashionId, ColorId]) ->
    #player_status{sid = Sid} = Player,
    case lib_fashion_check:equip_fashion_color_check(Player, PosId, FashionId, ColorId) of
        true ->
            lib_fashion:fashion_color_equip(Player, [PosId, FashionId, ColorId]);
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_413, 41302, [Errcode, PosId, FashionId, ColorId])
    end;

%% 卸下时装颜色
do_handle(41303, Player, [PosId, FashionId]) ->
    #player_status{sid = Sid} = Player,
    case lib_fashion_check:unequip_fashion_color_check(Player, PosId, FashionId) of
        true ->
            lib_fashion:fashion_color_unequip(Player, [PosId, FashionId]);
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_413, 41303, [Errcode, PosId, FashionId])
    end;

%% 时装颜色解锁/升星
do_handle(41306, Player, [PosId, FashionId, ColorId]) ->
    #player_status{sid = Sid} = Player,
    case lib_fashion_check:color_operate_check(Player, PosId, FashionId, ColorId) of
        {false, Errcode} ->
            lib_server_send:send_to_sid(Sid, pt_413, 41306, [Errcode, PosId, FashionId, 0, 0]),
            ok;
        {true, NewPlayer, OperateType, CostList} ->
            {ok, NewPlayer1} = lib_fashion:fashion_color_star_up(OperateType, NewPlayer, [PosId, FashionId, ColorId, CostList]),
            case OperateType of
                {?COLOR_STAR_UP, _} ->
                    {ok, NewPlayer1};
                _ ->
                    case do_handle(41302, NewPlayer1, [PosId, FashionId, ColorId]) of
                        {ok, NewPlayer2} ->
                            {ok, NewPlayer2};
                        _ ->
                            {ok, NewPlayer1}
                    end
            end;
        _ ->
            lib_server_send:send_to_sid(Sid, pt_413, 41306, [?FAIL, PosId, FashionId, 0, 0]),
            ok
    end;

%% 单个时装信息
do_handle(41310, Player, [PosId, FashionId]) ->
    #player_status{role_fashion = RoleFashion, sid = Sid} = Player,
    #role_fashion{all_fashions = AllFashions} = RoleFashion,
    #fashion_pos{wear_fashion_id = WearFashionId, wear_color_id = WearColorId, fashion_list = FashionList}
     = lib_fashion:get_fashion_pos(PosId, AllFashions),
    #fashion_info{color_list = ColorList, power = Power} = lib_fashion:get_fashion_info(PosId, FashionId, FashionList),
    {ok, BinData} = pt_413:write(41310, [PosId, FashionId, WearFashionId, WearColorId, Power, ColorList]),
    lib_server_send:send_to_sid(Sid, BinData),
    ok;

%% 套装信息
do_handle(41313, Player, _) ->
    #player_status{sid = Sid, role_fashion = RoleFashion} = Player,
    #role_fashion{suit_list = SuitList} = RoleFashion,
    SuitInfo = [{SuitId, [FashionId || {_PosId, FashionId} <- FashionL], Active} || {SuitId, FashionL, Active} <- SuitList],
    {ok, BinData} = pt_413:write(41313, [SuitInfo]),
    lib_server_send:send_to_sid(Sid, BinData),
    ok;

%% 激活套装
do_handle(41314, Player, [SuitId]) ->
    #player_status{sid = Sid, id = RoleId, role_fashion = RoleFashion} = Player,
    #role_fashion{all_fashions = AllFashions, suit_list = SuitList} = RoleFashion,
    case data_fashion:get_fashion_suit(SuitId) of
        #base_fashion_suit{fashion_id = NeedFashionL} ->
            case lists:keyfind(SuitId, 1, SuitList) of
                {_, SuitFashionL, Active} ->
                    Len = length(SuitFashionL),
                    NeedLen = length(NeedFashionL),
                    if Len >= NeedLen ->
                            if Active == 1 ->
                                    lib_server_send:send_to_sid(Sid, pt_413, 41314, [?ERRCODE(err413_have_actived)]);
                                true ->
                                    lib_fashion:db_update_fashion_suit(RoleId, SuitId, SuitFashionL, 1),
                                    NewSuitList = lists:keystore(SuitId, 1, SuitList, {SuitId, SuitFashionL, 1}),
                                    FashionAttrs   = lib_fashion:count_fashion_attr(AllFashions),
                                    SuitAttr       = lib_fashion:count_fashion_suit_attr(NewSuitList),
                                    FashionAttrs1  = lists:append(FashionAttrs, SuitAttr),
                                    FashionAttrs2  = lib_player_attr:partial_attr_convert(FashionAttrs1),
                                    NewRoleFashion = RoleFashion#role_fashion{suit_list = NewSuitList, fashion_attr = FashionAttrs2},
                                    NewPlayer = Player#player_status{role_fashion = NewRoleFashion},
                                    NewPlayer1  = lib_player:count_player_attribute(NewPlayer, ?MOD_FASHION, fashion_suit_active),
                                    lib_player:send_attribute_change_notify(NewPlayer1, ?NOTIFY_ATTR),
                                    lib_server_send:send_to_sid(Sid, pt_413, 41314, [?SUCCESS]),
                                    {ok, NewPlayer1}
                            end;
                        true ->
                            lib_server_send:send_to_sid(Sid, pt_413, 41314, [?ERRCODE(err413_suit_con_limit)])
                    end;
                _ ->
                    lib_server_send:send_to_sid(Sid, pt_413, 41314, [?ERRCODE(err413_suit_con_limit)])
            end;
        _ ->
            lib_server_send:send_to_sid(Sid, pt_413, 41314, [?FAIL])
    end;

do_handle(_Cmd, _Ps, _Args) ->
    ?ERR("fashion cmd unmatch ~p ~p", [_Cmd, _Args]),
    ok.