%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     拍照羁绊
%%% @end
%%%-------------------------------------------------------------------
-module(mate_take_photo).
-author("liushl").

-include("mate.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("server.hrl").
-include("daily.hrl").
-include("def_module.hrl").
-include("errcode.hrl").
%% API
-export([]).
-compile(export_all).

%% 羁绊-拍照 发出邀请
handle({send_invite, RoleId, InviteRoles}, State) ->
    #mate_mgr{mate_take_photo = TakePhoto} = State,
    IsSent = lists:keymember(RoleId, #mate_take_photo.id, TakePhoto),
    AllPosition = data_mate:get_all_pos_id(),
    Position = urand:list_rand(AllPosition),
    Res =
        if
            IsSent -> {false, 1};
            not is_integer(Position) -> {false, 2};
            true ->
                true
        end,
    case Res of
        {false, Error} ->
            lib_server_send:send_to_uid(RoleId, pt_149, 14945, [Error, 0]),
            State;
        true ->
            lib_server_send:send_to_uid(RoleId, pt_149, 14945, [1, Position]),
            One = #mate_take_photo{
                id        = RoleId,
                invite    = [#mate_take_photo_role{role_id = RoleId, is_accept = 1}]
                                ++ [#mate_take_photo_role{role_id = Id} || Id <- InviteRoles],
                position  = Position,
                time      = utime:unixtime()
            },
            NewTakePhoto = [One|TakePhoto],
            NewState = State#mate_mgr{mate_take_photo = NewTakePhoto},
            [
                handle({ask_invite_info, Id}, NewState) || Id <- InviteRoles
            ],
            NewState
    end;
%% 羁绊-拍照 收到的邀请信息
handle({ask_invite_info, RoleId}, State) ->
    #mate_mgr{mate_take_photo = TakePhoto} = State,
    SendInfo =
    lists:foldl(
        fun
            (#mate_take_photo{id = Id}, Temp) when Id =:= RoleId ->
                Temp;
            (#mate_take_photo{invite = Invite, id = Id, position = Position}, Temp) ->
                case lists:keymember(RoleId, #mate_take_photo_role.role_id, Invite) of
                    true  ->
                        [{lib_player:get_role_picture(Id), Position}|Temp];
                    false ->
                        Temp
                end
        end, [], TakePhoto
    ),
    {ok, Bin} = pt_149:write(14946, [SendInfo]),
    lib_server_send:send_to_uid(RoleId, Bin),
    State;
%% 羁绊-拍照 回应邀请
%% 拒绝会删掉我的信息
handle({response_invite, RoleId, FromId, _Position, ?MATE_RESPONSE_NO}, State) ->
    #mate_mgr{mate_take_photo = TakePhoto} = State,
    case lists:keyfind(FromId, #mate_take_photo.id, TakePhoto) of
        #mate_take_photo{invite = Invite} = One ->
            case lists:keyfind(RoleId, #mate_take_photo_role.role_id, Invite) of
                #mate_take_photo_role{is_accept = 0} ->
                    NewInvite = lists:keydelete(RoleId, #mate_take_photo_role.role_id, Invite),
                    NewTakePhoto = lists:keyreplace(FromId, #mate_take_photo.id, TakePhoto,
                        One#mate_take_photo{invite = NewInvite}),
                    NewState = State#mate_mgr{mate_take_photo = NewTakePhoto},
                    handle({ask_invite_info, RoleId}, NewState),
                    NewState;
                _ ->
                    State
            end;
        _ ->
            State
    end;
%% 同意
handle({response_invite, RoleId, FromId, Position, ?MATE_RESPONSE_YES}, State) ->
    #mate_mgr{mate_take_photo = TakePhoto} = State,
    case lists:keyfind(FromId, #mate_take_photo.id, TakePhoto) of
        #mate_take_photo{invite = Invite, position = Position} = One ->
            case lists:keyfind(RoleId, #mate_take_photo_role.role_id, Invite) of
                #mate_take_photo_role{is_accept = 0} = InviteInfo ->
                    NewInvite = lists:keyreplace(RoleId, #mate_take_photo_role.role_id, Invite,
                        InviteInfo#mate_take_photo_role{is_accept = 1}),
                    NewOne = One#mate_take_photo{invite = NewInvite},
                    NewTakePhoto = lists:keyreplace(FromId, #mate_take_photo.id, TakePhoto, NewOne),
                    NewState = State#mate_mgr{mate_take_photo = NewTakePhoto},
                    broadcast_role_info(NewOne),
                    NewState;
                #mate_take_photo_role{} ->
                    %% 已经同意了
                    State;
                _ ->
                    %% 没有邀请信息
                    handle({ask_invite_info, RoleId}, State),
                    State
            end;
        _ ->
            %% 数据过期
            handle({ask_invite_info, RoleId}, State),
            State
    end;
%% 羁绊-拍照 设置准备
handle({set_ready_status, RoleId, FromId, Status}, State) ->
    #mate_mgr{mate_take_photo = TakePhoto} = State,
    case lists:keyfind(FromId, #mate_take_photo.id, TakePhoto) of
        #mate_take_photo{invite = Invite} = One ->
            case lists:keyfind(RoleId, #mate_take_photo_role.role_id, Invite) of
                #mate_take_photo_role{is_accept = 1, is_ready = IsReady} = InviteInfo when IsReady =/= Status ->
                    set_role_lock(RoleId, Status),
                    NewInvite = lists:keyreplace(RoleId, #mate_take_photo_role.role_id, Invite,
                        InviteInfo#mate_take_photo_role{is_ready = Status}),
                    NewOne = One#mate_take_photo{invite = NewInvite},
                    NewTakePhoto = lists:keyreplace(FromId, #mate_take_photo.id, TakePhoto, NewOne),
                    NewState = State#mate_mgr{mate_take_photo = NewTakePhoto},
                    broadcast_role_info(NewOne),
                    NewState;
                _ ->
                    State
            end;
        _ ->
            State
    end;
%% 羁绊-拍照 拍照
handle({take_photo, RoleId}, State) ->
    #mate_mgr{mate_take_photo = TakePhoto} = State,
    case lists:keytake(RoleId, #mate_take_photo.id, TakePhoto) of
        {value, #mate_take_photo{invite = Invite} = One, Left} ->
            AnyReady = lists:any(fun(#mate_take_photo_role{is_ready = IsReady}) -> IsReady =:= 1 end, Invite),
            case AnyReady of
                true  ->
                    settlement(One),
                    NewState = State#mate_mgr{mate_take_photo = Left},
                    NewState;
                false ->
                    State
            end;
        _ ->
            State
    end;
%% 羁绊-拍照 取消游戏
handle({cancel, RoleId, FromId}, State) ->
    #mate_mgr{mate_take_photo = TakePhoto} = State,
    case RoleId =:= FromId of
        true  ->
            %% 发起者取消，则直接结束活动
            case lists:keytake(RoleId, #mate_take_photo.id, TakePhoto) of
                {value, #mate_take_photo{} = One, Left} ->
                    act_end(One),
                    NewState = State#mate_mgr{mate_take_photo = Left},
                    NewState;
                _ ->
                    State
            end;
        false ->
            %% 受邀者取消，则自己结束活动
            case lists:keyfind(FromId, #mate_take_photo.id, TakePhoto) of
                #mate_take_photo{invite = Invite} = One ->
                    case lists:keymember(RoleId, #mate_take_photo_role.role_id, Invite) of
                        true ->
                            act_end(RoleId),
                            NewInvite = lists:keydelete(RoleId, #mate_take_photo_role.role_id, Invite),
                            NewOne = One#mate_take_photo{invite = NewInvite},
                            NewTakePhoto = lists:keyreplace(FromId, #mate_take_photo.id, TakePhoto, NewOne),
                            NewState = State#mate_mgr{mate_take_photo = NewTakePhoto},
                            broadcast_role_info(NewOne),
                            NewState;
                        false ->
                            State
                    end;
                _ ->
                    State
            end
    end;
handle(_Msg, State) ->
    State.

%% 广播活动信息
broadcast_role_info(#mate_take_photo{invite = Invite, position = Position}) ->
    RoleInfo =
        [
            {lib_player:get_role_picture(RoleId), IsReady}
            || #mate_take_photo_role{role_id = RoleId, is_accept = IsAccept, is_ready = IsReady} <- Invite, IsAccept > 0
        ],
    {ok, Bin} = pt_419:write(14948, [RoleInfo, Position]),
    [
        lib_server_send:send_to_uid(RoleId, Bin) || {#rec_picture{id = RoleId}, _} <- RoleInfo
    ],
    ok.

%% 结束活动
act_end(#mate_take_photo{invite = Invite}) ->
    [
        act_end(RoleId) ||#mate_take_photo_role{role_id = RoleId, is_accept = IsAccept} <- Invite, IsAccept > 0
    ];
act_end(RoleId) ->
    lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?MODULE, notice_act_status, [0]).

%% 通知玩家活动状态
notice_act_status(Player, Status) ->
    {ok, Bin} = pt_149:write(14952, [Status]),
    lib_server_send:send_to_sid(Player#player_status.sid, Bin).

%% 给玩家设置玩法锁
set_role_lock(RoleId, ?MATE_READY_YES) ->
    lib_player:setup_action_lock(RoleId, ?ERRCODE(err149_photo_ready));
set_role_lock(RoleId, ?MATE_READY_NO) ->
    lib_player:break_action_lock(RoleId, ?ERRCODE(err149_photo_ready)).

%% 结算
settlement(#mate_take_photo{invite = Invite, id = Owner}) ->
    Num = length([1 || #mate_take_photo_role{is_ready = IsReady} <- Invite, IsReady > 0]),
    [
        lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?MODULE, settlement_in_role, [Num, Owner =:= RoleId])
        ||#mate_take_photo_role{role_id = RoleId, is_ready = IsReady} <- Invite, IsReady > 0
    ],
    ok.

%% 玩家进程结算
settlement_in_role(Player, Num, true) ->
    lib_daily:increment(Player#player_status.id, ?MOD_MATE, ?MATE_COUNTER_INVITE),
    lib_daily:increment(Player#player_status.id, ?MOD_MATE, ?MATE_COUNTER_BE_INVITED),
    case data_mate:get_photo_award(1) of
        {OwnerAward, _} when OwnerAward =/= [] ->
            settlement_do(Player, Num, OwnerAward);
        _ ->
            Player
    
    end;
settlement_in_role(Player, Num, false) ->
    DailyCount = lib_daily:get_count(Player#player_status.id, ?MOD_MATE, ?MATE_COUNTER_BE_INVITED),
    case data_mate:get_photo_award(DailyCount + 1) of
        {_, OtherAward} when OtherAward =/= [] ->
            lib_daily:increment(Player#player_status.id, ?MOD_MATE, ?MATE_COUNTER_BE_INVITED),
            settlement_do(Player, Num, OtherAward);
        _ ->
            Player
    
    end.

settlement_do(Player, Num, Award) ->
    notice_act_status(Player, 0),
    lib_server_send:send_to_sid(pt_149, 14951, [Num, Award]),
    Player1 = lib_goods_api:send_reward(Player, Award, mate_photo),
    lib_player:break_action_lock(Player1, ?ERRCODE(err149_photo_ready)).