%% ---------------------------------------------------------------------------
%% @doc lib_guild_shop_mod.erl

%% @author  zmh
%% @since  2019-12-10
%% @deprecated 社团商店
%% ---------------------------------------------------------------------------
-module(lib_guild_shop_mod).
-export([]).
-compile(export_all).

-include("guild.hrl").
-include("guild_labor.hrl").
-include("sql_guild.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("figure.hrl").
-include("def_fun.hrl").
-include("def_module.hrl").
-include("def_event.hrl").
-include("rec_event.hrl").
-include("server.hrl").
-include("goods.hrl").


%%数据加载
login(RoleID,Now)->
    Sql = io_lib:format(?SQL_ROLE_GUILD_SHOP_GET, [RoleID]),
    GuildShop  = case db:get_row(Sql) of
        [CargosD0,DailyL0,CargosW0,WeekL0,CargosF0] ->
            CargosD1 = util:bitstring_to_term(CargosD0),
            CargosW1 = util:bitstring_to_term(CargosW0),
            CargosF1 = util:bitstring_to_term(CargosF0),
            CargosD2  = case is_list(CargosD1) of
                true->
                    CargosD1;
                _->
                    []
            end,
            CargosW2  = case is_list(CargosW1) of
                true->
                    CargosW1;
                _->
                    []
            end,
            CargosF  = case is_list(CargosF1) of
                true->
                    CargosF1;
                _->
                    []
            end,
            {CargosD,DailyL} = case utime:is_logic_same_day(DailyL0) of
                true->
                    {CargosD2,DailyL0};
                _->
                    {[],Now}
            end,
            Monday = utime:logic_week_start(),
            {CargosW,WeekL} = case utime:logic_week_start(WeekL0) == Monday of
                true->
                    {CargosW2,WeekL0};
                _->
                    {[],Now}
            end,
            #role_guild_shop{
                cargos_daily= CargosD,
                time_daily  = DailyL,
                cargos_week = CargosW,
                time_week   = WeekL,
                cargos_forever = CargosF
            };
        _ ->
            #role_guild_shop{time_daily=Now,time_week=Now}
    end,
    GuildShop.

handle_event(Player, #event_callback{type_id = ?EVENT_FOUR_CLOCK}) when is_record(Player, player_status) ->
    #player_status{
        id = RoleID,
        guild_shop = GuildShop
    } = Player,
    #role_guild_shop{
        cargos_week  = CargosW0,
        time_week = WeekL0,
        cargos_forever = CargosF
    }=GuildShop,
    Now = utime:unixtime(),
    [CargosW,WeekL] = case utime:day_of_week()=:=1 of
        true->
            [[],utime:logic_week_start(Now+100)];
        _->
            [CargosW0,WeekL0]
    end,
    NGuildShop = GuildShop#role_guild_shop{
        cargos_daily = [],
        time_daily  = Now,
        cargos_week  = CargosW,
        time_week = WeekL,
        cargos_forever = CargosF
    },
    NPlayer = Player#player_status{guild_shop=NGuildShop},
    save_role_guild_cargos(RoleID,NGuildShop),
    get_cargos(NPlayer),
    {ok, NPlayer};
handle_event(Player, _Event) ->
    {ok, Player}.

handle_event(_Event) ->
    ok.

%%获取已购买情况
get_cargos(Status)->
	#player_status{
        id = RoleID,
        guild_shop = GuildShop
    } = Status,
	#role_guild_shop{
        cargos_daily = CargosD,
        cargos_week  = CargosW,
        cargos_forever = CargosF
    }=GuildShop,
    Cargos = CargosD++CargosW++CargosF,
    {ok,Bin} = pt_400:write(40027,[Cargos]),
    lib_server_send:send_to_uid(RoleID,Bin).

%%购买
buy_goods(Status,Id,Num)->
	#player_status{
		id = RoleID,
        guild_shop = GuildShop,
		figure=#figure{lv=RoleLv},
		guild =#status_guild{lv=GLv}
	}=Status,
	Config = data_guild_labor:get_guild_cargo(Id),
	CKList = [
        {check_config,Config},
        {check_limit,Config,RoleLv,GLv},
        {check_max,Config,GuildShop,Id,Num}
    ],
    case util:check_lists(?MODULE,CKList) of
        {ok,{CargosD,CargosW,CargosF}}->
        	#guild_cargo_base{cost=Cost0,cargo=Gain0}=Config,
            Cost = [{K,T,V*Num}||{K,T,V}<-Cost0],
            Gain = [{K,T,V*Num}||{K,T,V}<-Gain0],
        	case lib_goods_api:cost_object_list_with_check(Status, Cost, guild_shop, ["guild shop buy"]) of
        		{true,Status0,_}->
        			Produce = #produce{type=guild_shop,reward=Gain},
                    lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_GUILD),
                    NGuildShop = GuildShop#role_guild_shop{
                    	cargos_daily = CargosD,
        				cargos_week  = CargosW,
        				cargos_forever = CargosF
                    },
                    NStatus = Status0#player_status{guild_shop=NGuildShop},
                    save_role_guild_cargos(RoleID,NGuildShop),
        			Res = ?SUCCESS;
        		{false,Err,_}->
        			NStatus = Status,
        			Res = Err
        	end;
        {false,Err}->
        	Res = Err,
        	NStatus=Status
   end,
   {ok,Bin} = pt_400:write(40028,[Res,Id,Num]),
   lib_server_send:send_to_uid(RoleID,Bin),
   NStatus.

check({check_config,Config})->
	case is_record(Config,guild_cargo_base) of
		true->
			true;
		_->
			%%系统未开放
			{false,?ERRCODE(err_config)}
	end;
check({check_limit,Config,RoleLv,GLv})->
	#guild_cargo_base{
		lv_min=LvMin,
		lv_max=LvMax,
		guild_lv=Need
	}=Config,
	case LvMin=<RoleLv andalso RoleLv=<LvMax of
		true->
			case GLv>=Need of
				true->
					true;
				_->
					{false,?ERRCODE(err400_guild_lv_not_enough)}
			end;
		_->
			{false,?ERRCODE(err400_role_lv_not_reached)}
	end;
check({check_max,Config,GuildShop,Id,Num})->
	#role_guild_shop{
        cargos_daily = CargosD,
        cargos_week  = CargosW,
        cargos_forever = CargosF
    }=GuildShop,
    #guild_cargo_base{
		type=Type,
		num=Limit
	}=Config,
	case Type of
		0->
			{ok,{CargosD,CargosW,CargosF}};
		1->
			CurV = case lists:keyfind(Id,1,CargosD) of
				{_,V}->
					V;
				_->
					0
			end,
			case CurV+Num>Limit of
				true->
					{false,?ERRCODE(err400_limit_buy_more)};
				_->
					{ok,{lists:keystore(Id,1,CargosD,{Id,CurV+Num}),CargosW,CargosF}}
			end;
		2->
			CurV = case lists:keyfind(Id,1,CargosW) of
				{_,V}->
					V;
				_->
					0
			end,
			case CurV+Num>Limit of
				true->
					{false,?ERRCODE(err400_limit_buy_more)};
				_->
					{ok,{CargosD,lists:keystore(Id,1,CargosW,{Id,CurV+Num}),CargosF}}
			end;
		_->
			CurV = case lists:keyfind(Id,1,CargosF) of
				{_,V}->
					V;
				_->
					0
			end,
			case CurV+Num>Limit of
				true->
					{false,?ERRCODE(err400_limit_buy_more)};
				_->
					{ok,{CargosD,CargosW,lists:keystore(Id,1,CargosF,{Id,CurV+Num})}}
			end
	end.


save_role_guild_cargos(RoleID,GuildShop)->
	#role_guild_shop{
        cargos_daily= CargosD1,
        time_daily  = DailyL,
        cargos_week = CargosW1,
        time_week   = WeekL,
        cargos_forever = CargosF1
    }=GuildShop,
    CargosD = util:term_to_string(CargosD1),
    CargosW = util:term_to_string(CargosW1),
    CargosF = util:term_to_string(CargosF1),
    SQL = io_lib:format(?SQL_ROLE_GUILD_SHOP_INSERT, [RoleID,CargosD,DailyL,CargosW,WeekL,CargosF]),
    db:execute(SQL).
