%%% @author root <root@localhost.localdomain>
%%% @copyright (C) 2018, root
%%% @doc
%%%
%%% @end
%%% Created :  3 Feb 2018 by root <root@localhost.localdomain>
-module(pp_designation).

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

handle(Cmd, PlayerStatus, Data) ->
    #player_status{figure = Figure} = PlayerStatus,
    OpenLv = lib_module:get_open_lv(?MOD_DESIGNATION),
    case Figure#figure.lv >= OpenLv of
        true ->
            do_handle(Cmd, PlayerStatus, Data);
        false -> skip
    end.

%% 获取称号列表
do_handle(41101, PlayerStatus, []) ->
    #player_status{sid = Sid, dsgt_status = DsgtStatus} = PlayerStatus,
    {List, ExpireList} = lib_designation:get_player_dsgt(DsgtStatus),
    F = fun(RmDsgtId, PSTmp) ->
            {ok, PsTmp} = lib_designation:cancel_dsgt(PSTmp, RmDsgtId),
            PsTmp
        end,
    NewPS = lists:foldl(F, PlayerStatus, ExpireList),
    Figure = NewPS#player_status.figure,
    % ?ERR("~p", [List]),
    {ok, BinData} = pt_411:write(41101, [Figure#figure.designation, List]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewPS};

%% 佩戴称号
do_handle(41102, PlayerStatus, [DsgtId]) ->
    #player_status{id = PlayerId, sid = Sid, figure = Figure, dsgt_status = DsgtStatus,
                  scene = SceneId, scene_pool_id = ScenePId, copy_id = CopyId, x = X, y = Y} = PlayerStatus,
    UsedDsgtId = Figure#figure.designation,
    #dsgt_status{dsgt_map = DsgtMap} = DsgtStatus,
    CheckList = [
        {check_change_same_id, {DsgtId, UsedDsgtId}},
        {check_id_valid, {DsgtId, DsgtMap}},
        {check_is_expire, {DsgtId, DsgtMap}}
    ],
    case lib_designation_util:checklist(CheckList) of
        true ->
            NewDsgtStatus = lib_designation:change_dsgt(DsgtId, Figure#figure.designation, DsgtStatus),
            mod_scene_agent:update(PlayerStatus, [{designation, DsgtId}]),
            NewFigure = Figure#figure{designation = DsgtId},
            NewPs = PlayerStatus#player_status{figure = NewFigure, dsgt_status = NewDsgtStatus},
            lib_role:update_role_show(PlayerId, [{designation, DsgtId}]),
            %% 佩戴成功
            {ok, BinData1} = pt_411:write(41102, [?CODE_OK, DsgtId]),
            {ok, BinData2} = pt_411:write(41105, [PlayerId, DsgtId]),
            lib_server_send:send_to_sid(Sid, <<BinData1/binary, BinData2/binary>>),
            %% 场景广播
            % ?ERR("~p ~p ~p ~p ~p", [PlayerId, Sid, {SceneId, ScenePId, CopyId, {X, Y}}, DsgtId, BinData2]),
            lib_server_send:send_to_area_scene(SceneId, ScenePId, CopyId, X, Y, BinData2),
            {ok, NewPs};
        {fail, ErrCode} ->
            {ok, BinData} = pt_411:write(41102, [ErrCode, 0]),
            lib_server_send:send_to_sid(Sid, BinData)
    end;

%% 卸下称号
do_handle(41103, PlayerStatus, [DsgtId]) ->
    #player_status{id = RoleId, sid = Sid, figure = Figure, dsgt_status = DsgtStatus,
                   scene = SceneId, scene_pool_id = ScenePId, copy_id = CopyId, x = X, y = Y} = PlayerStatus,
    UsedDsgtId = Figure#figure.designation,
    CheckList = [
        {check_hide_current, {DsgtId, UsedDsgtId}},
        {check_current_exist, {DsgtId, DsgtStatus#dsgt_status.dsgt_map}}
    ],
    case lib_designation_util:checklist(CheckList) of
        true ->
            NewDsgtStatus = lib_designation:hide_dsgt(DsgtId, DsgtStatus),
            mod_scene_agent:update(PlayerStatus, [{designation, 0}]),
            NewFigure = Figure#figure{designation = 0},
            NewPs = PlayerStatus#player_status{dsgt_status = NewDsgtStatus, figure = NewFigure},
            lib_role:update_role_show(RoleId, [{designation, 0}]),
            {ok, BinData} = pt_411:write(41103, [?CODE_OK]),
            lib_server_send:send_to_sid(Sid, BinData),
            %% 场景广播
            {ok, BinData2} = pt_411:write(41105, [RoleId, 0]),
            lib_server_send:send_to_area_scene(SceneId, ScenePId, CopyId, X, Y, BinData2),
            {ok, NewPs};
        {fail, ErrCode} ->
            {ok, BinData} = pt_411:write(41103, [ErrCode]),
            lib_server_send:send_to_sid(Sid, BinData)
    end;

%% 主动激活称号
do_handle(41106, PlayerStatus, [GoodsTypeId, DsgtId, GoodsNum]) -> %?ERR("~p ~p ~p", [GoodsTypeId, DsgtId, GoodsNum]),
    #player_status{sid = Sid, dsgt_status = #dsgt_status{dsgt_map = DsgtMap}} = PlayerStatus,
    GoodsStatus = lib_goods_do:get_goods_status(),
    CheckList = [
        {check_designation_config, {DsgtId, GoodsTypeId}},
        {check_id_active, {DsgtId, GoodsTypeId, DsgtMap}},
        {check_cost_lists, PlayerStatus, [{?TYPE_GOODS, GoodsTypeId, GoodsNum}]}
    ],
    case lib_designation_util:checklist(CheckList) of
        true ->
            case check_designation_use(GoodsTypeId, GoodsNum, GoodsStatus) of
                true ->
                    Res = case lib_designation:use_dsgt_goods(PlayerStatus, #goods{goods_id = GoodsTypeId}, GoodsNum) of
                        {ok, NewPS0, GoodsNum} ->
                            {ok, NewPS0, GoodsNum};
                        {use_designtion_good, NewPS0, _SellPrice, GoodsNum} ->
                            {ok, NewPS0, GoodsNum};
                        {fail, Err} ->
                            {false, Err}
                    end,
                    case Res of
                        {ok, NewPS, GoodsNum} ->
                            lib_server_send:send_to_sid(Sid, pt_411, 41106, [?SUCCESS, DsgtId]),
                            {ok, NewPS};
                        {false, Err1} ->
                            lib_server_send:send_to_sid(Sid, pt_411, 41106, [Err1, DsgtId]),
                            ok
                    end;
                {false, Err} -> %?ERR("~p", [Err]),
                    {ok, BinData} = pt_411:write(41106, [Err, DsgtId]),
                    lib_server_send:send_to_sid(Sid, BinData),
                    ok;
                _ ->
                    {ok, BinData} = pt_411:write(41106, [0, DsgtId]),
                    lib_server_send:send_to_sid(Sid, BinData),
                    ok
            end;
        {fail, Err} -> %?ERR("~p", [Err]),
            {ok, BinData} = pt_411:write(41106, [Err, DsgtId]),
            lib_server_send:send_to_sid(Sid, BinData),
            ok
    end;

do_handle(_, PlayerStatus, _) ->
    {ok, PlayerStatus}.


check_designation_use(GoodsTypeId, GoodsNum, GoodsStatus) ->
    #goods_status{player_id = RoleId, player_lv = RoleLv, dict = Dict} = GoodsStatus,
    GoodsList = lib_goods_util:get_type_goods_list(RoleId, GoodsTypeId, ?GOODS_LOC_BAG, Dict, RoleLv),
    TotalNum = lib_goods_util:get_goods_total_num(GoodsList),
    {_, _, IsOverLay} = lib_designation_api:get_dsgt(GoodsTypeId),
    if GoodsNum > TotalNum ->
            {false, ?ERRCODE(goods_not_enough)};
        true ->
            case lists:any(fun(G) ->
                if
                    not is_record(G, goods) ->
                        true;
                    true ->
                        false
                end
             end, GoodsList) of
                false ->
                    if IsOverLay == 0 andalso GoodsNum > 1 ->
                            {false, ?ERRCODE(err411_dgst_can_not_overlay)};
                        true ->
                            true
                    end;
                true ->
                    {false, ?ERRCODE(err150_require_err)}
            end
    end.

