require( "scripts/common/simple_object.lua" );

local	indicator_pos_x = 30;
local	indicator_pos_y = -30;

local ObjectTable = GetObjectTable("toybox");


toybox2 = simple_object:new{
	current_action_character="youngchild",
	anchor_x=15,
	anchor_y=41,
	offset_x=-24,
	offset_y=-2,
	indicator_ui_x=indicator_pos_x,
	indicator_ui_y=indicator_pos_y,
	dad_ui_x=indicator_pos_x-35,
	dad_ui_y=indicator_pos_y,
	mom_ui_x=indicator_pos_x+35,
	mom_ui_y=indicator_pos_y,
	texture="anim/objects/toybox/toybox",
	animation_script="anim/objects/default_animation_script.lua",
	glow_texture="anim/objects/toybox/toybox_glow",
	--glow_animation_script="anim/objects/default_glow_animation_script.lua",
	flipped_animation = false,

	messing_state_time = 3000,
	cleaning_state_time = 3000,
	
	show_cleaning_particle = false;
	
	toys_x = "-50";
	toys_y = "-80";
	toys_layer = "1";
	
	flip_animation_parent = false,
	
	toys_on_floor = 0,
	
	complex_object = true;
	dad_next_task_x = indicator_pos_x-15,
	dad_next_task_y = indicator_pos_y,
	mom_next_task_x = indicator_pos_x+15,
	mom_next_task_y = indicator_pos_y,
		
	dad_next_task_showing = false;
	mom_next_task_showing = false;
	
	sound_of_messing = "mess_toybox";
	
	already_increased_stress = false;

	messing_state_time = ObjectTable["messing_time"],
	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = ObjectTable["stress_inc"],

}

function toybox2:new(o)
	o = o or {}   -- create object if user does not provide one
	setmetatable(o, self)
	self.__index = self
	return o
end

function toybox2:clean_init()
	self.object_is_dirty = false;
	self.click_enable = self.set_click_enable;	
	
	if self.click_enable then
		IncludeObjectMouseHandler(self.object_name);
	end
	
	self.parent_is_coming = false;
	
	PlayGlowAnimation(self.object_name, "clean");
	
	PlayObjectAnimation(self.object_name, self.object_clean_animation);
	
	self.previous_state = "clean";
	
	if self.complex_object then
		HideNextTaskIcon(self.object_name);
		HideDadNextTaskIcon(self.object_name);
		HideMomNextTaskIcon(self.object_name);
	end
end

function toybox2:clean_update()
	if GetNumberOfHoldingItems("dad") > 0 then
		if not self.dad_next_task_showing then
			self.dad_next_task_showing = true;
			ShowDadNextTaskIcon(self.object_name);
		end
	else
		if self.dad_next_task_showing then
			self.dad_next_task_showing = false;
			HideDadNextTaskIcon(self.object_name);
		end
	end
	
	if GetNumberOfHoldingItems("mom") > 0 then
		if not self.mom_next_task_showing then
			self.mom_next_task_showing = true;
			ShowMomNextTaskIcon(self.object_name);
		end
	else
		if self.mom_next_task_showing then
			self.mom_next_task_showing = false;
			HideMomNextTaskIcon(self.object_name);
		end
	end
end

function toybox2:click()
	if self.click_enable then
		--self.clicked = true;
		PlaySound("check", false);
		self.parent_chosen = GetSelectedParent();
		--DebugOut(self.object_name..":"..self.parent_chosen);
		self.no_stress_power = IsNannyStressPowerActive();
		
		if self.no_stress_power then
			ShowNoStressIcon(self.object_name);
		end
		
		if self.parent_chosen == "dad" then
			ShowDadIcon(self.object_name);
		else	
			ShowMomIcon(self.object_name);
		end
		
		if self.object_is_dirty then
			PlayGlowAnimation(self.object_name, "dirty_click");
			if self.stress_inc_enabled and not self.no_stress_power then
				AddFutureCharacterStress(self.parent_chosen, self.stress_inc);
			end	
		else
			if self.parent_chosen == "dad" then
				if self.dad_next_task_showing then
					AddFutureCharacterStress(self.parent_chosen, self.stress_inc);
					self.already_increased_stress = true;
				end
			else	
				if self.mom_next_task_showing then
					AddFutureCharacterStress(self.parent_chosen, self.stress_inc);
					self.already_increased_stress = true;
				end
			end			
		end
		
		self:parent_command();
		self.click_enable = false;
		self.parent_is_coming = true;
		self.previous_state = GetObjectState(self.object_name);
		
		IncludeObjectInClickedList(self.object_name);
		--self.parent_current_state = GetCharacterState(self.parent_chosen);
		--self.parent_current_stuff = GetCharacterStuff(self.parent_chosen);
	end
end

function toybox2:dirty_init()
	self.object_is_dirty = true;
	
	PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
	--PlayGlowAnimation(self.object_name, "dirty");
	self.toys_on_floor = GetNumberOfToysOnTheFloor();
	
	self.previous_state = GetObjectState(self.object_name);
	
	if not self.parent_is_coming then
		self.click_enable = true;
		IncludeObjectMouseHandler(self.object_name);
	else
		self.click_enable = false;
		--self.previous_state = GetObjectState(self.object_name);
		PlayGlowAnimation(self.object_name, "dirty_click");
		if self.stress_inc_enabled and not self.no_stress_power then
			if not self.already_increased_stress then
				AddFutureCharacterStress(self.parent_chosen, self.stress_inc);
			end
		end
	end
	
	if self.check_teamwork then
		ShowTeamWorkIcon(self.object_name, ShouldShowCharacterTeamWork(self.teamworktask_name));
		--ShowTeamWorkIcon(self.object_name, "mom", ShouldShowCharacterTeamWork("mom", self.teamworktask_name));
	end
end

function toybox2:dirty_update()	
	if GetNumberOfHoldingItems("dad") > 0 then
		if not self.dad_next_task_showing then
			self.dad_next_task_showing = true;
			ShowDadNextTaskIcon(self.object_name);
		end
	else
		if self.dad_next_task_showing then
			self.dad_next_task_showing = false;
			HideDadNextTaskIcon(self.object_name);
		end
	end
	
	if GetNumberOfHoldingItems("mom") > 0 then
		if not self.mom_next_task_showing then
			self.mom_next_task_showing = true;
			ShowMomNextTaskIcon(self.object_name);
		end
	else
		if self.mom_next_task_showing then
			self.mom_next_task_showing = false;
			HideMomNextTaskIcon(self.object_name);
		end
	end
end

function toybox2:parent_action()
	self.parent_is_coming = false;
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	
	RemoveObjectFromClickedList(self.object_name);
	
	if GetCharacterState(self.parent_chosen) == "holding_toy" and GetNumberOfHoldingItems(self.parent_chosen) > 0 then	
		local n = GetNumberOfHoldingItems(self.parent_chosen);
		--while n > 0 do
		
		
		--don't change the sort of the 3 functions below:
		l = GetNumberOfToysOnTheFloor();
		SetNumberOfToysOnTheFloor(l - n);
		FinishTeamWorkTask(self.parent_chosen, "pickuptoys", n);
		--don't change the sort of the 3 functions above
		
		
			--n = n - 1;
		--end
		
		if(self.parent_chosen) == "dad" then
			HideDadNextTaskIcon(self.object_name);
			self.dad_next_task_showing = false;
		else
			HideMomNextTaskIcon(self.object_name);
			self.mom_next_task_showing = false;
		end
		HideCharacterStuff(self.parent_chosen);
		SetCharacterState(self.parent_chosen, "normal");
		ClearNumberOfHoldingItems(self.parent_chosen);
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		
		
		
		if self.object_is_dirty then
			self:cleantoybox();
		else
			if self.stress_inc_enabled and not self.no_stress_power then
				AddCharacterStress(self.parent_chosen, self.stress_inc);
			end
			self:cancelparentaction();
		end
	else
		if GetCharacterState(self.parent_chosen) == self.parent_state_action and self.object_is_dirty then
			self:cleantoybox();
		else
			if self.stress_inc_enabled and not self.no_stress_power then
				AddFutureCharacterStress(self.parent_chosen, -self.stress_inc);
			end
			self:cancelparentaction();			
			ShowMiss(self.object_name);
		end
	end
end

function toybox2:cleantoybox()
	if self.parent_chosen == "dad" then
		PlaySound("working_father", true);
	else
		PlaySound("working_mother", true);
	end
	PlayCharacterAnimation(self.parent_chosen, self.character_cleaning_animation);
	FlipAnimation(self.parent_chosen, self.flip_animation_parent);
	PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
	ShowCharacterStuff(self.parent_chosen, "organizing_toys", self.toys_x, self.toys_y, self.toys_layer);
	ChangeObjectState(self.object_name, self.cleaning_state_name);
	self.new_state_start_time = GetCurrentTime();
	SetCharacterState(self.parent_chosen, self.cleaning_state_name);
	self:get_cleaning_state_time();
end

function toybox2:cancelparentaction()
	
	if self.no_stress_power then
		HideNoStressIcon(self.object_name);
	end
	self.no_stress_power = false;
	IncludeObjectMouseHandler(self.object_name);
	FinishCharacterTask(self.parent_chosen);
	self.parent_chosen = "";
	self.click_enable = self.set_click_enable;
	if self.object_is_dirty then
		--ChangeObjectState(self.object_name, self.dirty_state_name);			
		IncludeObjectMouseHandler(self.object_name);			
		self.click_enable = true;
		PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
		--PlayGlowAnimation(self.object_name, "dirty");
	end
end

function toybox2:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		if self.parent_chosen == "dad" then
			StopSound("working_father");
			PlaySound("task_complete_father", false);
		else
			StopSound("working_mother");
			PlaySound("task_complete_mother", false);
		end
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		HideCharacterStuff(self.parent_chosen);
		if self.stress_inc_enabled and not self.no_stress_power then
			AddCharacterStress(self.parent_chosen, self.stress_inc);
		end
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		FinishCharacterTask(self.parent_chosen);
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		SetCharacterState(self.parent_chosen, "normal");
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
		
		self.already_increased_stress = false;
		
		
		FinishTeamWorkTask(self.parent_chosen, "messes", 1);
	end
end

function simple_object:chainglow_youngchild_action(character_name)
	self.current_action_character = character_name;	
	self:clean_mess_action();
end

function simple_object:chainglow_toddler_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function simple_object:chainglow_schoolaged_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end