Delegate.RegInit("Signin_Init");

--@e@êĺΔc挦
signInCountPrize = {1,2,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,56,61,66,71,76,81,86,91,96,100};
--@e@õĪcͬ
--Ō g|Ŕߌ i|ID|֣ f|
signInPrize = {"i|87633|ħ10","i|87634|ħ20","i|87635|ħ30","i|830038|ʱˮLV1","g|10000","i|830042|ħLv2","g|15000","i|87636|ħ40","g|25000","i|830039|ʱˮLv3","i|87637|ħ50","i|830039|ʱˮLv3","i|87637|ħ50","g|30000","i|830039|ʱˮLv3","i|87639|ħ70","i|830043|ħLv3","i|830040|ʱˮLv6","i|87642|ħ100","i|87629|ħLv2","i|87629|ħLv2","i|87629|ħLv2","i|87629|ħLv2","i|87629|ħLv2","i|87630|ħLv3","i|87630|ħLv3","i|87630|ħLv3","i|87630|ħLv3","i|87630|ħLv3"};
--		1			2		3			6		9		12		15		18		21		24		27			30			33		36		39			42			45			48		51	

function Signin_Init()
	signin_create();
	print(tonumber(Field.Get(_tome,"SigninTime")));
end

function signin_create() --ÿǩ
	if(SigninNPC == nil)then
		SigninNPC = NL.CreateNpc("./lua/System/BaseModule/Base.lua", "Myinit");
		Char.SetData(SigninNPC,%_%,14604);
		Char.SetData(SigninNPC,%_ԭ%,14604);
		Char.SetData(SigninNPC,%_X%,247);
		Char.SetData(SigninNPC,%_Y%,86);
		Char.SetData(SigninNPC,%_ͼ%,1000);
		Char.SetData(SigninNPC,%_%,6);
		Char.SetData(SigninNPC,%_%,"ÿǩ");
		NLG.UpChar(SigninNPC);
		Char.SetWindowTalkedEvent("./lua/Module/Task_Signin.lua","SigninNPCWinTalked",SigninNPC);
		Char.SetTalkedEvent("./lua/Module/Task_Signin.lua","SigninNPCTalked", SigninNPC);
	end
end

function canSignin(_tome)
	local times = tonumber(Field.Get(_tome,"SigninCount"));
	local signinTime = tonumber(Field.Get(_tome,"SigninTime"));
	local signble = 0;
	
	if(times == nil and signinTime == nil )then
		signble = 1;
	end

	if(times ~= nil and signinTime ~= nil)then
		local nowTime = os.time();
		local CTimeM = os.date("%m",signinTime);
		local CTimeD = os.date("%d",signinTime);
		local BTimeM = os.date("%m",nowTime);
		local BTimeD = os.date("%d",nowTime);
		if(CTimeM == BTimeM and CTimeD == BTimeD)then
			--Ѿǩ
			signble = 0;
			return signble,times;
		end
		if(CTimeM ~= BTimeM or CTimeD ~= BTimeD)then		
			local signintimediff = os.difftime(nowTime,signinTime);
			if(signintimediff > 86400 and signintimediff < 172800)then
			--ǩ
				signble = 1;
			end
			if(signintimediff >= 172800)then
			--ǩ,ǩ
				signble = 1;
				Field.Set(_tome,"SigninCount","0");
				times = 0;
			end
		end
	end
	return signble,times;
end

function SigninNPCTalked(_me,_tome)

	if(NLG.CanTalk(_me,_tome)==false)then
		return;
	end
	
	local myStr = "";
	local signble,times = canSignin(_tome);
	
	if(signble == 0 or signble == nil)then
		myStr = "\\n\\n"..Char.GetData(_tome,%_%)..",ӭħ\\n\\n".."ÿǩϵͳ\\n\\nѾǩ,ǳ!\\n\\nĿǰѾǩ "..times.." Ŷ!\\n";
		NLG.ShowWindowTalked(_tome,_me,%_Ϣ%,%ť_ر%,0,myStr);
		return;
	end

	if (times == nil)then
		myStr = "\\n\\n"..Char.GetData(_tome,%_%)..",ӭħ\\n".."ÿǩϵͳ\\nƺǵһʹǩ\\nǩ,òͬĽ\\nĽǰ̳鿴\\nַ̳http://www.35doo.com/\\nھͽǩô?";
		NLG.ShowWindowTalked(_tome,_me,%_Ϣ%,%ť_ȷر%,0,myStr);
		return;
	end
	if(signble == 1 and times > 0)then
		myStr = "\\n\\n"..Char.GetData(_tome,%_%)..",ӭħ\\n".."ÿǩϵͳ\\nѾǩ "..times.." Ŷ!\\nھͽǩô?";
		NLG.ShowWindowTalked(_tome,_me,%_Ϣ%,%ť_ȷر%,0,myStr);
		return;
	end
	if(signble == 1 and times == 0)then
		myStr = "\\n\\n"..Char.GetData(_tome,%_%)..",ӭħ\\n".."ÿǩϵͳ\\nƺһʱûħ!\\n֪ÿɺ?\\n,ϣھ!\\nھͽǩô?";
		NLG.ShowWindowTalked(_tome,_me,%_Ϣ%,%ť_ȷر%,0,myStr);
		return;
	end
end

function makeSignin(_me,_tome,times)
	local myStr = "\\n\\n\\nϲǩɹ!\\nл֧־ħ,ףϷ!\\n";
	local prizeCount = IsInTable(times,signInCountPrize);
	if(prizeCount ~= -1)then
		local prizeSetting = Split(signInPrize[prizeCount],"|");
		if(prizeSetting[1] == "g")then
			Char.AddGold(_tome,tonumber(prizeSetting[2]));
			NLG.SystemMessage(_tome,"ýǮ "..tonumber(prizeSetting[2]).." Gold.");
		end
		if(prizeSetting[1] == "i")then
			if(Char.ItemSlot(_tome) == 20)then
				NLG.ShowWindowTalked(_tome,_me,%_Ϣ%,%ť_ر%,0,"\\n\\nıƺװ½Ŷ!\\n\\nȥ°!");
				return;
			end
			Char.GiveItem(_tome,tonumber(prizeSetting[2]));
			NLG.SystemMessage(_tome,"õ "..prizeSetting[3].." .");
		end
		if(prizeSetting[1] == "f")then
			Char.SetData(_tome,%_%,Char.GetData(_tome,%_%)+tonumber(prizeSetting[2]));
			NLG.UpChar(_tome);
			NLG.SystemMessage(_tome," "..tonumber(prizeSetting[2]));
		end
		myStr = myStr .. "ĽѾ,עŶ!\\n";
	end
	local t = os.time();
	local m = os.date("%m" ,t);
	local d = os.date("%d" ,t);
	local Y = os.date("%Y" ,t);
	local tt = {year = Y, month = m, day = d, hour = 0};
	Field.Set(_tome,"SigninTime",""..os.time(tt));
	Field.Set(_tome,"SigninCount",""..times);
	NLG.ShowWindowTalked(_tome,_me,%_Ϣ%,%ť_ر%,0,myStr);
end

function SigninNPCWinTalked(_me,_tome,_seqno,_select,_data)
	if(NLG.CanTalk(_me,_tome) == false)then
		return;
	end
	
	if(_select == %ť_ȷ% or _select == ""..%ť_ȷ%)then
		local signble,times = canSignin(_tome);
		if(times == nil)then
			times = 0;
			signble = 1;
		end
		if(signble==1)then
			times = times + 1;
			makeSignin(_me,_tome,times);
		end
	end
end