%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     物品合成进化处理
%%% @end
%%%-------------------------------------------------------------------
-module(goods_compose_evolution).
-author("liushl").

-include("goods.hrl").
-include("errcode.hrl").
-include("def_goods.hrl").
-include("server.hrl").
-include("equipment.hrl").
-include("figure.hrl").
-compile(export_all).

%% API
-export([]).
%%-------------------------------------------------------------------------------------
%% @doc 根据合成分类判断合成条件
%%-------------------------------------------------------------------------------------
%% 装备进化:忽略前端发的材料列表，直接读配置
goods_compose_or_evolution_check_by_classify(
    Type,
    #base_goods_compose_or_evolution_info{
        cost_goods = CostList
    } = Config,
    GoodsInfo,
    _CostList,
    _Times,
    Player
) when Type =:= ?GOODS_COMPOSE_EVOLUTION_GUARD
    orelse Type =:= ?GOODS_COMPOSE_EVOLUTION_JEWELRY ->
    case equipment_api:equipment_evolution_check(GoodsInfo) of
        {false, Err} -> {false, Err};
        true ->
            goods_compose_or_evolution_check_by_classify(
                ?GOODS_COMPOSE_EVOLUTION_NORMAL,
                Config,
                GoodsInfo,
                CostList,
                1,
                Player
            )
    end;

%% 通用判断:会根据前端发的消耗列表处理
goods_compose_or_evolution_check_by_classify(
    ?GOODS_COMPOSE_EVOLUTION_SPECIAL,
    #base_goods_compose_or_evolution_info{
        cost_goods = CostGoods,
        base_ratio = Ratio,
        target_goods_id = TargetGoodsId,
        cost_money = CostMoney
    } = Config,
    GoodsInfo,
    CostList,
    Times,
    _Player
) ->
    %% 判断消耗列表
    {List, CostGoodsList} = format_goods(CostList),
    GoodsEnough = lib_goods_util:goods_object_multiply_by(CostGoods, Times) -- List,
    Money = lib_goods_util:goods_object_multiply_by(CostMoney, Times),
    if
        GoodsEnough =/= [] -> {false, ?ERRCODE(goods_not_enough)};
        true ->
            {ok, Config, Ratio, GoodsInfo, Money, List, CostGoodsList, TargetGoodsId, Times}
    end;

%% 普通合成，根据配置消耗材料
goods_compose_or_evolution_check_by_classify(
    _,
    #base_goods_compose_or_evolution_info{
        cost_goods = CostGoods,
        base_ratio = Ratio,
        target_goods_id = TargetGoodsId,
        cost_money = CostMoney
    } = Config,
    GoodsInfo,
    _CostList,
    Times,
    _Player
) ->
    Cost = lib_goods_util:goods_object_multiply_by(CostGoods, Times),
    Money = lib_goods_util:goods_object_multiply_by(CostMoney, Times),
    {ok, Config, Ratio, GoodsInfo, Money, Cost, Cost, TargetGoodsId, Times};
goods_compose_or_evolution_check_by_classify(_, _, _, _, _, _) ->
    {false, 0}.

format_goods(GoodsList) ->
    GoodsStatus = lib_goods_do:get_goods_status(),
    format_goods(GoodsStatus, GoodsList, [], []).
format_goods(_, [], List, CostList) -> {List, CostList};
format_goods(GoodsStatus, [{GoodsId, TypeId, Num}|T], List, CostInfo) ->
    case lib_goods_api:get_goods_info_by_id_type(GoodsStatus, GoodsId, TypeId) of
        #goods{num = HasNum} = GoodsInfo when HasNum >= Num ->
            NewList = case lists:keyfind(TypeId, 2, List) of
                          {?TYPE_GOODS, TypeId, OldNum} ->
                              lists:keyreplace(TypeId, 2, List, {?TYPE_GOODS, TypeId, OldNum + Num});
                          false ->
                              [{?TYPE_GOODS, TypeId, Num}|List]
                      end,
            format_goods(GoodsStatus, T, NewList, [{GoodsInfo, Num}|CostInfo]);
        _ ->
            format_goods(GoodsStatus, T, List, CostInfo)
    end.

%%-------------------------------------------------------------------------------------
%% @doc 进化 新物品特殊操作
%%-------------------------------------------------------------------------------------
%% 装备进化
goods_evolution_by_classify(#base_goods_compose_or_evolution_info{classify_id = Classify},
    #goods{type = ?GOODS_TYPE_EQUIP} = FromGoods, NewGoodsInfo)
    when Classify =:= ?GOODS_COMPOSE_EVOLUTION_GUARD orelse
    Classify =:= ?GOODS_COMPOSE_EVOLUTION_JEWELRY ->
    equipment_api:equipment_evolution(FromGoods, NewGoodsInfo);
%% 普通进化
goods_evolution_by_classify(_, _, NewGoodsInfo) ->
    NewGoodsInfo.

%%-------------------------------------------------------------------------------------
%% @doc 进化成功后的操作
%%-------------------------------------------------------------------------------------
%% 装备进化
goods_evolution_after_by_classify(#base_goods_compose_or_evolution_info{classify_id = Classify},
    Player, FromGoods, #goods{type = ?GOODS_TYPE_EQUIP} = NewGoods)
    when Classify =:= ?GOODS_COMPOSE_EVOLUTION_GUARD orelse
    Classify =:= ?GOODS_COMPOSE_EVOLUTION_JEWELRY ->
    equipment_api:after_equipment_evolution(Player, FromGoods, NewGoods);
%% 橙装合成
goods_evolution_after_by_classify(#base_goods_compose_or_evolution_info{classify_id = Classify} = Cfg,
    Player, _, _) when Classify =:= ?GOODS_COMPOSE_ORANGE_EQUIPMENT ->
    ta_agent_fire:orange_compose(Player, Cfg),
    Player;
goods_evolution_after_by_classify(_, Player, _, _) ->
    Player.

%%-------------------------------------------------------------------------------------
%% @doc 合成条件是否开放
%%-------------------------------------------------------------------------------------
goods_compose_or_evolution_check_condition([], _) -> true;
goods_compose_or_evolution_check_condition([{open_lv, OpenLv}|T], Player) ->
    #player_status{figure = #figure{lv = Lv}} = Player,
    if Lv >= OpenLv ->
            goods_compose_or_evolution_check_condition(T, Player);
        true ->
            {false, ?ERRCODE(err150_lv_gfit_cfg_err)}
    end;
goods_compose_or_evolution_check_condition([{opday, OpDay}|T], Player) ->
    OpenDay = util:get_open_day(),
    if OpenDay >= OpDay ->
            goods_compose_or_evolution_check_condition(T, Player);
        true ->
            {false, ?ERRCODE(err157_9_open_day_limit)}
    end;
goods_compose_or_evolution_check_condition([_H|T], Player) ->
    goods_compose_or_evolution_check_condition(T, Player).