/*
=============================================
sellshop
Created by: Murilo - BiO'
================================================
v1.0.1 Fixed some warnings
Opens a Sell shop with the desired item type to be sold
*/
#include "common/hercules.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "common/HPMi.h"
#include "common/mmo.h"
#include "common/socket.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/mapindex.h"
#include "common/strlib.h"
#include "common/utils.h"
#include "common/cbasetypes.h"
#include "common/timer.h"
#include "common/showmsg.h"
#include "common/conf.h"
#include "common/db.h"
#include "common/sql.h"

#include "map/atcommand.h"
#include "map/battle.h"
#include "map/clif.h"
#include "map/map.h"
#include "map/npc.h"
#include "map/skill.h"
#include "map/itemdb.h"
#include "map/script.h"
#include "map/pc.h"

#include "common/HPMDataCheck.h"

HPExport struct hplugin_info pinfo = {
	"sellshop",			// Plugin name
	SERVER_TYPE_MAP,	// Which server types this plugin works with?
	"1.0.1",			// Plugin version
	HPM_VERSION,		// HPM Version (don't change, macro is automatically updated)
};

void my_clif_selllist(struct map_session_data *sd, int fature, int type1, int type2, int type3, int type4, int type5, int type6, int type7, int type8, int type9, int type10, int type11)
{
	int fd,i,c=0,val;

	nullpo_retv(sd);

	fd = sd->fd;
	WFIFOHEAD(fd, MAX_INVENTORY * 10 + 4);
	WFIFOW(fd,0)=0xc7;
	for( i = 0; i < MAX_INVENTORY; i++ )
	{
		if( sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] )
		{
			if(type1 && itemdb_type(sd->status.inventory[i].nameid) != IT_HEALING)
				continue;
			else if(type2 && itemdb_type(sd->status.inventory[i].nameid) != IT_USABLE)
				continue;
			else if(type3 && itemdb_type(sd->status.inventory[i].nameid) != IT_ETC)
				continue;
			else if(type4 && itemdb_type(sd->status.inventory[i].nameid) != IT_WEAPON)
				continue;
			else if(type5 && itemdb_type(sd->status.inventory[i].nameid) != IT_ARMOR)
				continue;
			else if(type6 && itemdb_type(sd->status.inventory[i].nameid) != IT_CARD)
				continue;
			else if(type7 && itemdb_type(sd->status.inventory[i].nameid) != IT_PETEGG)
				continue;
			else if(type8 && itemdb_type(sd->status.inventory[i].nameid) != IT_PETARMOR)
				continue;
			else if(type9 && itemdb_type(sd->status.inventory[i].nameid) != IT_AMMO)
				continue;
			else if(type10 && itemdb_type(sd->status.inventory[i].nameid) != IT_DELAYCONSUME)
				continue;
			else if(type11 && itemdb_type(sd->status.inventory[i].nameid) != IT_CASH)
				continue;
			
			if( !itemdb_cansell(&sd->status.inventory[i], pc_get_group_level(sd)) )
				continue;

			if( sd->status.inventory[i].expire_time )
				continue; // Cannot Sell Rental Items

			if( sd->status.inventory[i].bound && !pc_can_give_bound_items(sd))
				continue; // Don't allow sale of bound items

			val=sd->inventory_data[i]->value_sell;
			if( val < 0 )
				continue;
			WFIFOW(fd,4+c*10)=i+2;
			WFIFOL(fd,6+c*10)=val;
			WFIFOL(fd,10+c*10)= fature ? pc->modifysellvalue(sd,val) : val;
			c++;
		}
	}
	WFIFOW(fd,2)=c*10+4;
	WFIFOSET(fd,WFIFOW(fd,2));
}

int my_npc_buysellsel(struct map_session_data* sd, int id, int fature, int type1, int type2, int type3, int type4, int type5, int type6, int type7, int type8, int type9, int type10, int type11) {
	struct npc_data *nd;

	nullpo_retr(1, sd);

	if ((nd = npc->checknear(sd,map->id2bl(id))) == NULL)
		return 1;

	if ( nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP && nd->subtype != SHOP ) {
		ShowError("my_npc_buysellsel: no such shop npc %d (%s)\n",id,nd->exname);

		if (sd->npc_id == id)
			sd->npc_id = 0;
		return 1;
	}

	if (nd->option & OPTION_INVISIBLE) // can't buy if npc is not visible (hack?)
		return 1;

	if( nd->class_ < 0 && !sd->state.callshop ) {// not called through a script and is not a visible NPC so an invalid call
		return 1;
	}

	if (nd->subtype == ITEMSHOP) {
		char output[CHAT_SIZE_MAX];
		struct item_data *itd = itemdb->exists(nd->u.shop.itemshop_nameid);
		memset(output,'\0',sizeof(output));
		if (itd) {
			sprintf(output,msg_sd(sd,714),itd->jname,itd->nameid); // Item Shop List: %s (%hu)
			clif->broadcast(&sd->bl,output,strlen(output) + 1,BC_BLUE,SELF);
		}
	} else if (nd->subtype == POINTSHOP) {
		char output[CHAT_SIZE_MAX];
		memset(output,'\0',sizeof(output));
		sprintf(output,msg_sd(sd,715),nd->u.shop.pointshop_str); // Point Shop List: '%s'
		clif->broadcast(&sd->bl,output,strlen(output) + 1,BC_BLUE,SELF);
	}
	
	// reset the callshop state for future calls
	sd->state.callshop = 0;
	sd->npc_shopid = id;

	my_clif_selllist(sd, fature,type1,type2,type3,type4,type5,type6,type7,type8,type9,type10,type11);

	return 0;
}

/**
 * usage: sellshop <"shop_name">,<OverCharge>{,<Healing>,<Consumable>,<Etc>,<Weapon>,<Armor>,<Card>,<PetEgg>,<PetArmor>,<Ammo>,<DelayConsume>,<Cash>}
 **/
BUILDIN(sellshop)
{
	struct npc_data *nd;
	struct map_session_data *sd = script->rid2sd(st);
	const char *shopname;
	int fature;
	int type1 = 1;  //IT_HEALING
	int type2 = 1;  //IT_USABLE
	int type3 = 1;  //IT_ETC
	int type4 = 1;  //IT_WEAPON
	int type5 = 1;  //IT_ARMOR
	int type6 = 1;  //IT_CARD
	int type7 = 1;  //IT_PETEGG
	int type8 = 1;  //IT_PETARMOR
	int type9 = 1;  //IT_AMMO
	int type10 = 1; //IT_DELAYCONSUME
	int type11 = 1; //IT_CASH

	if (sd == NULL)
		return true;
	shopname = script_getstr(st, 2);
	
	nd = npc->name2id(shopname);
	if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != ITEMSHOP && nd->subtype != POINTSHOP) )
	{
		ShowError("buildin_sellshop: Shop [%s] not found (or NPC is hasn't sell function)\n", shopname);
		script_pushint(st,0);
		return false;
	}
	
	fature = script_getnum(st,3); //0/1 -> on/off
	if( script_hasdata(st,4) ) type1 = script_getnum(st,4);
	if( script_hasdata(st,5) ) type2 = script_getnum(st,5);
	if( script_hasdata(st,6) ) type3 = script_getnum(st,6);
	if( script_hasdata(st,7) ) type4 = script_getnum(st,7);
	if( script_hasdata(st,8) ) type5 = script_getnum(st,8);
	if( script_hasdata(st,9) ) type6 = script_getnum(st,9);
	if( script_hasdata(st,10) ) type7 = script_getnum(st,10);
	if( script_hasdata(st,11) ) type8 = script_getnum(st,11);
	if( script_hasdata(st,12) ) type9 = script_getnum(st,12);
	if( script_hasdata(st,13) ) type10 = script_getnum(st,13);
	if( script_hasdata(st,14) ) type11 = script_getnum(st,14);

	// flag the user as using a valid script call for opening the shop (for floating NPCs)
	sd->state.callshop = 1;
	
	my_npc_buysellsel(sd,nd->bl.id,fature,type1,type2,type3,type4,type5,type6,type7,type8,type9,type10,type11);

	sd->npc_shopid = nd->bl.id;
	script_pushint(st,1);
	return true;
}

/* run when server starts */
HPExport void plugin_init (void) {
	addScriptCommand("sellshop","si???????????",sellshop);
}