%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(pp_god).
-author("liushl").

-include("common.hrl").
-include("server.hrl").
-include("god.hrl").
-include("figure.hrl").
-include("def_module.hrl").

%% API
-export([handle/3]).

handle(Cmd, Player, Data) ->
    Lv = Player#player_status.figure#figure.lv,
    Limit = lib_module:get_open_lv(?MOD_GOD),
    case Lv >= Limit of
        true -> do_handle(Cmd, Player, Data);
        false ->
            ok
    end.

%%  式神信息
do_handle(Cmd = 17301, Player, _) ->
    #player_status{sid = Sid, role_god = RoleGod} = Player,
    #role_god{
        god_info = GodInfo, unlock = UnLock, role_power = RolePower
    } = RoleGod,
    GodList =
        [{GodId, Star, Lv, Exp, Power, Skills, Pos} ||
            #god{
                id = GodId,         % 式神id
                star = Star,        % 式神星级
                lv = Lv,            % 式神等级
                exp = Exp,          % 式神经验
                pos = Pos,          % 出战位置
                skills = Skills,    % 技能等级[{skill_id, lv}|_]
                power_show = Power
            } <- GodInfo],
    Info = [RolePower, GodList, UnLock],
    lib_server_send:send_to_sid(Sid, pt_173, Cmd, Info),
    ok;

%%  式神信息：单条刷新
do_handle(Cmd = 17302, Player, [GodId]) ->
    #player_status{sid = Sid, role_god = RoleGod} = Player,
    #role_god{
        god_info = GodInfo    %% 玩家激活的式神
    } = RoleGod,
    case lists:keyfind(GodId, #god.id, GodInfo) of
        #god{
            id = GodId,         % 式神id
            star = Star,        % 式神星级
            lv = Lv,            % 式神等级
            exp = Exp,          % 式神经验
            pos = Pos,          % 出战位置
            skills = Skills,    % 技能等级[{skill_id, lv}|_]
            power_show = Power
        } ->
            Info = [GodId, Star, Lv, Exp, Power, Skills, Pos],
            lib_server_send:send_to_sid(Sid, pt_173, Cmd, Info);
        _ ->
            ok
    end,
    ok;

%% 式神操作：激活
do_handle(Cmd = 17303, Player, [GodId]) ->
    case god_logic:god_active(Player, GodId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, GodId]);
        {ok, NewPlayer} ->
            do_handle(17312, NewPlayer, []),
            {ok, NewPlayer}
    end;

%% 式神操作：升星
do_handle(Cmd = 17304, Player, [GodId]) ->
    case god_logic:god_star_up(Player, GodId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, GodId, 0]);
        {ok, NewPlayer, GodStar} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, GodId, GodStar]),
            {ok, NewPlayer}
    end;

%% 式神操作：升级
do_handle(Cmd = 17305, Player, [GodId, UpType, GoodsId, Num]) ->
    case god_logic:god_lv_up(Player, GodId, UpType, GoodsId, Num) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, GodId, 0, 0, 0]);
        {ok, NewPlayer, [NewLv, NewExp, Change]} ->
            lib_task_api:god_lv_up(NewPlayer),
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, GodId, NewLv, NewExp, util:boolean_to_integer(Change)]),
            {ok, NewPlayer}
    end;

%% 式神操作：技能升级
do_handle(Cmd = 17306, Player, [GodId, _]) ->
    case god_logic:god_skill_up(Player, GodId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, GodId, 0, 0]);
        {ok, NewPlayer, {SkillId, SkillLv}} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, GodId, SkillId, SkillLv]),
            {ok, NewPlayer}
    end;

%% 式神操作：穿戴装备 pt_17307_[1,6,506806160791]
do_handle(Cmd = 17307, Player, [GodId, Pos, GoodsId]) ->
    case god_logic:god_equip(Player, GodId, Pos, GoodsId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, GodId, Pos, GoodsId]);
        {ok, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, GodId, Pos, GoodsId]),
            {ok, NewPlayer}
    end;

%% 式神操作：装备卸下 pt_17308_[1,1]
do_handle(Cmd = 17308, Player, [GodId, Pos]) ->
    case god_logic:god_equip_remove(Player, GodId, Pos) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, GodId, Pos]);
        {ok, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, GodId, Pos]),
            {ok, NewPlayer}
    end;

%% 式神操作：装备升级 pt_17309_[541165907707,[{541165907708,1}]]
do_handle(Cmd = 17309, Player, [GoodsId, CostList]) ->
    case god_logic:god_equip_upgrade(Player, GoodsId, CostList) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, GoodsId]);
        {ok, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, GoodsId]),
            {ok, NewPlayer}
    end;

%% 式神操作：解锁位置
do_handle(Cmd = 17310, Player, [Pos]) ->
    case god_logic:unlock_pos(Player, Pos) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, Pos]);
        {ok, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, Pos]),
            {ok, NewPlayer}
    end;

%% 式神操作：上阵
do_handle(Cmd = 17311, Player, [Pos, GodId]) ->
    case god_logic:god_front(Player, GodId, Pos) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, Pos, GodId]);
        {ok, NewPlayer} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, Pos, GodId]),
            {ok, NewPlayer}
    end;

%% 式神信息：上次召唤时间戳
do_handle(Cmd = 17312, Player, _) ->
    #player_status{sid = Sid, role_god = RoleGod} = Player,
    #role_god{summon_time = SummonTime} = RoleGod,
    lib_server_send:send_to_sid(Sid, pt_173, Cmd, [SummonTime]),
    ok;

%%  式神操作：召唤出战/重复点击切换下一个
do_handle(Cmd = 17313, Player, [GodId]) ->
    case god_logic:god_summon(Player, GodId) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, 0, 0]);
        {ok, NewPlayer} ->
            {ok, NewPlayer}
    end;

%% 抽奖信息
do_handle(_Cmd = 17315, Player, _) ->
    god_lottery:ask_limit_lottery_info(Player#player_status.id),
    ok;

%% 抽奖
do_handle(Cmd = 17316, Player, [Type, IsAotuBuy]) when Type == 1 orelse Type == 2 ->
    case god_logic:god_lottery(Player, Type, IsAotuBuy) of
        {false, Err} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [Err, Type, [], 0]);
        {ok, NewPlayer, Award, Blessing} ->
            lib_server_send:send_to_sid(Player#player_status.sid, pt_173, Cmd, [1, Type, Award, Blessing]),
            {ok, NewPlayer}
    end;

%% 抽奖:领取保底奖励
do_handle(_Cmd = 17317, Player, [AwardId]) ->
    god_lottery:counts_award(Player#player_status.id, AwardId),
    ok;

%% 抽奖:领取累计奖励
do_handle(_Cmd = 17318, Player, [Stage]) ->
    god_lottery:blessing_award(Player#player_status.id, Stage),
    ok;

%% 宠物生活技能的每日免费刷新次数
do_handle(17321, Status, [])->
    FreeTimes = lib_pet_api:pet_life_skill_about_god_summon_free(Status),
    UseTimes  = lib_daily:get_count(Status#player_status.id, ?MOD_GOD, 1),
    lib_server_send:send_to_sid(Status#player_status.sid, pt_173, 17321,
        [max(0, FreeTimes-UseTimes)]
    ),
    {ok, Status};

do_handle(Cmd, Player, Info) ->
    ?ERR("pp_god err:~p~n", [{Cmd, Player#player_status.id, Info}]),
    ok.
