%%%-------------------------------------------------------------------
%%% @author zmh
%%% @doc
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(lib_role_glamour).
-include("server.hrl").
-include("common.hrl").
-include("scene.hrl").
-include("errcode.hrl").
-include("chat.hrl").
-include("def_module.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("glamour.hrl").

-compile(export_all).
%% API
-export([
    trans_focuser_role/1,
    get_focus_data/1,
    get_top_focuser_show/1,
    use_item/3,
    buy_items/3,
    guess_runer/3,
    auto_chat/3
]).

%% 数据转换
trans_focuser_role(Status) ->
    #player_status{
        id = RoleID,
        figure = Figure
    } = Status,
    %% 宠物生活技能，获得额外初始资金
    ExtraMoney = lib_pet_api:pet_life_skill_about_glamour_fund_add(Status),
    #glamour_focuser{
        rid = RoleID,
        figure = Figure,
        money = data_glamour:get(init_money) + ExtraMoney,
        total = data_glamour:get(init_money) + ExtraMoney,
        items = data_glamour:get(init_item)
    }.

get_focus_data(Status)->
    Focuser = lib_role_glamour:trans_focuser_role(Status),
    mod_glamour_focus:get_focus_data(Focuser).

use_item(RunID,Item,Status)->
    Focuser = lib_role_glamour:trans_focuser_role(Status),
    case catch mod_glamour_ctrl:get_time() of
        {ok,?S_RUN,_ETime}->
            mod_glamour_focus:use_item(RunID,Item,Focuser);
        _->
            {ok,Bin} = pt_142:write(14209,[0,0,?ERRCODE(err142_cant_use_now),0]),
            lib_server_send:send_to_uid(Focuser#glamour_focuser.rid,Bin)
    end.

buy_items(Item,AimId,Status)->
    #player_status{id=RoleID}=Status,
    {NStatus,Result} = case catch mod_glamour_ctrl:get_time() of
        {ok,?S_RUN,_ETime}->
            case data_glamour:get_item(Item) of
                #glamour_item{price=Cost}->
                    case catch mod_glamour_focus:get_role_focuster(RoleID) of
                        {ok,none} ->
                            {Status,?ERRCODE(err142_cant_buy_now)};
                        {ok,Items}->
                            case lists:keyfind(Item,1,Items) of
                                {_,V} when V>0->
                                    {Status,?ERRCODE(err142_cant_buy_now)};
                                _->
                                    case lib_goods_api:cost_object_list_with_check(Status, Cost, glamour_buy_item, "") of
                                        {true, Status2,_} ->
                                            Focuser = lib_role_glamour:trans_focuser_role(Status),
                                            mod_glamour_focus:buy_items(RoleID,Item,Focuser),
                                            {Status2,?SUCCESS};
                                        {false, ErrorCode, _Status2} ->
                                            {Status, ErrorCode}
                                    end
                            end
                    end;
                _->
                    {Status,?ERRCODE(missing_config)}
            end;
        _->
            {Status,?ERRCODE(err142_cant_buy_now)}
    end,
    {ok,Bin} = pt_142:write(14212,[Result,Item,AimId]),
    lib_server_send:send_to_uid(RoleID,Bin),
    NStatus.


get_top_focuser_show(Status)->
    #player_status{id=RoleID}=Status,
    mod_glamour_focus:get_top_focuser_show(RoleID).

%%投注
guess_runer(RunID,Add,Status)->
    Focuser = lib_role_glamour:trans_focuser_role(Status),
    IsLikeT = case catch mod_glamour_ctrl:get_time() of
        {ok,?S_LIKE,_}->
            true;
        {ok,?S_RUN,ETime}->
            RunT = data_glamour:get(run_time),
            LikeExt = data_glamour:get(like_ext),
            Now = utime:unixtime(),
            NETime = ETime-RunT+LikeExt,
            Now=<NETime;
        _->
            false
    end,
    IsLikeT andalso lib_achievement_api:glamour_event(Status, 1),
    mod_glamour_focus:guess_runer(RunID,Add,IsLikeT,Focuser).

get_focus_view(Status)->
    Focuser = lib_role_glamour:trans_focuser_role(Status),
    mod_glamour_focus:get_focus_view(Focuser).

%%道具使用成功自动说话
auto_chat(Status,RName,Item)->
    case data_glamour:get_item(Item) of
        #glamour_item{msgs=Msgs} when Msgs /= []->
            Msg0 = urand:list_rand(Msgs),
            Msg  = lists:concat(["<color=#10aaf1>",uio:thing_to_string(RName),"</color>",Msg0]),
            #player_status{server_id = SerId, server_num = SerNum, server_name = ServerName, id = Id, figure = Figure} = Status,
            Channel = ?CHAT_CHANNEL_GLAMOUR,
            NowTime = utime:unixtime(),
            PackData = [Channel, SerId, SerNum, ServerName, Id, Figure, Msg, util:term_to_string([]), ?CHAT_ARGS_RESULT_PASS, NowTime],
            PackData2 = [Channel, SerId, SerNum, ServerName, Id, Figure, Msg, util:term_to_string([]), ?CHAT_ARGS_RESULT_PASS, NowTime, 0, 0],
            BinData = lib_chat:make_data(PackData2),
            {ok, NewStatus} = lib_chat:send_text_msg(Status, ?CHAT_CONTENT_TEXT, Channel, 0, PackData, BinData),
            NewStatus;
        _->
            Status
    end.

check({check_money_enough,Money,Add})->
    case Add=<Money andalso Add>0 of
        true->
            true;
        _->
            {false,?ERRCODE(err142_has_no_enough_money)}
    end;
check({check_bet_time,IsLikeT})->
    case IsLikeT of
        true->
            true;
        _->
            {false,?ERRCODE(err142_not_guess_time)}
    end;
check({check_runer_buff,Runer})->
    #glamour_runer{buffs=Buffs}=Runer,
    case length(Buffs)>0 of
        true->
            {false,?ERRCODE(err142_still_item_buff)};
        _->
            true
    end;
check({check_runer_item_cd,Runer})->
    #glamour_runer{btime=BTime}=Runer,
    case utime:unixtime()=<BTime of
        true->
            {false,?ERRCODE(err142_buff_cding)};
        _->
            true
    end;
check({check_runer_exit,Runer})->
    case is_record(Runer,glamour_runer) of
        true->
            true;
        _->
            {false,?ERRCODE(err142_find_no_runer)}
    end.