%%%-------------------------------------------------------------------
%%% author : ljy
%%% desc   : 舞姿
%%% Time   : 2021.04.08
%%%-------------------------------------------------------------------
-module(lib_dance).
-author("ljy").

%% API
-export([
    login/1
    , dance_show/1
    , dance_activate/2
    , dance_switch/2
    , dance_rand/2
    , enter_dance_state/1
]).

-include("dance.hrl").
-include("server.hrl").
-include("errcode.hrl").

login(RoleId) ->
    case db:get_row(?SQL_DANCE_SELECT, [RoleId]) of
        [IsRand, DanceId, DanceList| _] ->
            #dance{is_rand = IsRand, dance_id = DanceId, dance_list = util:bitstring_to_term(DanceList)};
        _ ->
            DanceId = ?DEFAULT_DANCE,
            db:execute(io_lib:format(?SQL_DANCE_UPDATE, [RoleId, 0, DanceId, util:term_to_bitstring([DanceId])])),
            #dance{is_rand = 0, dance_id = DanceId, dance_list = [DanceId]}
    end.

%% 舞姿总览
dance_show(Player) ->
    #player_status{sid = Sid, dance = Dance} = Player,
    #dance{is_rand = IsRand, dance_id = DanceId, dance_list = DanceList} = Dance,
    {ok, Bin} = pt_425:write(42500, [IsRand, DanceId, DanceList]),
    lib_server_send:send_to_sid(Sid, Bin).

%% 舞姿激活
dance_activate(Player, Did) ->
    #player_status{id = RoleId, sid = Sid, dance = Dance} = Player,
    #dance{is_rand = IsRand, dance_list = DanceList} = Dance,
    case check(Did, DanceList) of
        true ->
            #base_dance{goods = Goods} = data_dance:dance_cfg(Did),
            case lib_goods_api:cost_object_list_with_check(Player, Goods, dance_activate, "") of
                {true, Player1, _} ->
                    NDanceList = [Did | DanceList],
                    NewDance = Dance#dance{dance_id = Did, dance_list = NDanceList},
                    db:execute(io_lib:format(?SQL_DANCE_UPDATE, [RoleId, IsRand, Did, util:term_to_bitstring(NDanceList)])),
                    NewPlayer = Player1#player_status{dance = NewDance},
                    lib_server_send:send_to_sid(Sid, pt_425, 42502, [?SUCCESS, Did]),
                    {ok, NewPlayer};
                {false, ErrCode, _Player1} ->
                    {false, ErrCode}
            end;
        {false, ErrCode} ->
            {false, ErrCode}
    end.

check(Did, DanceList) ->
    CfgDance = data_dance:dance_cfg(Did),
    IsActivate = lists:member(Did, DanceList),
    if
        IsActivate -> {false, ?ERRCODE(err_425_already_activate)};
        is_record(CfgDance, base_dance) -> true;
        true -> {false, ?ERRCODE(missing_config)}
    end.

%% 切换舞姿
dance_switch(Player, Did) ->
    #player_status{id = RoleId, sid = Sid, scene = SceneId, scene_pool_id = ScenePoolId,
        copy_id = CopyId, role_dance = RoleDance, dance = Dance} = Player,
    #dance{is_rand = IsRand, dance_id = DanceId, dance_list = DanceList} = Dance,
    IsHas = lists:member(Did, DanceList),
    if
        IsHas ->
            NewDance = Dance#dance{dance_id = Did},
            DanceId =/= Did andalso db:execute(io_lib:format(?SQL_DANCE_UPDATE, [RoleId, IsRand, Did, util:term_to_bitstring(DanceList)])),
            if
                RoleDance == 0 ->
                    NewPlayer = Player#player_status{sid = Sid, dance = NewDance};
                true ->
                    mod_scene_agent:change_dance(SceneId, ScenePoolId, CopyId, RoleId, Did),
                    NewPlayer = Player#player_status{role_dance = Did,dance = NewDance}
            end,
            {ok, NewPlayer};
        true ->
            {false, ?ERRCODE(err_425_not_activate)}
    end.

%% 是否随机舞姿
dance_rand(Player, Type) ->
    #player_status{id = RoleId, sid = Sid, dance = Dance} = Player,
    #dance{dance_id = DanceId, dance_list = DanceList} = Dance,
    NewDance = Dance#dance{is_rand = Type},
    NewPlayer = Player#player_status{sid = Sid, dance = NewDance},
    db:execute(io_lib:format(?SQL_DANCE_UPDATE, [RoleId, Type, DanceId, util:term_to_bitstring(DanceList)])),
    {ok, Bin} = pt_425:write(42503, [Type]),
    lib_server_send:send_to_sid(Player#player_status.sid, Bin),
    {ok, NewPlayer}.

%% 进入舞姿、晚宴时前端请求进入跳舞状态
enter_dance_state(Player) ->
    #player_status{id = RoleId, scene = SceneId, scene_pool_id = ScenePoolId,
        copy_id = CopyId,sid = Sid, dance = Dance} = Player,
    #dance{dance_id = DanceId, is_rand = Type, dance_list = DanceList} = Dance,
    if
        Type == 1 andalso is_list(DanceList) ->
            Did = urand:list_rand(DanceList);
        true ->
            Did = DanceId
    end,
    lib_server_send:send_to_sid(Sid, pt_120, 12025, [?SUCCESS, Did]),
    mod_scene_agent:change_dance(SceneId, ScenePoolId, CopyId, RoleId, Did),
    ok.
