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

local	indicator_pos_x = -5;
local	indicator_pos_y = -20;

local ObjectTable = GetObjectTable("familytimetime");

family_time = simple_object:new{
	character_cleaning_animation = "doing_stuff_up",
	parent_state_action = "normal",
	anchor_x=7,
	anchor_y=140,
	--offset_x=-9,
	--offset_y=-8,
	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,
	
	cleaning_state_time = 6000;
	
	all_kids_sat = false;
	all_family_together = false;
	
	node_to_dad_go = "";
	node_to_mom_go = "";
		
	set_click_enable = false;
	
	child_members = {};
	
	texture="anim/objects/familytime/familytime",
	animated = true,
	
	mask = true;
	maskX = 0;
	maskY = 50;
	maskWidth = 218;
	maskHeight = 200;
	maskAlpha = 0.01;
	
	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_coming = false;
	mom_coming = false;
	
	dad_current_state = "";
	mom_current_state = "";
	
	dad_current_stuff = "";
	mom_current_stuff = "";
	
	stress_inc_enabled = false;

	cleaning_state_time = ObjectTable["cleaning_time"],
	
	is_a_mess = false,
}

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

function family_time:cancel()	
	--self.click_enable = self.set_click_enable;
	--IncludeObjectMouseHandler(self.object_name);
	
	if self.dad_coming then
		if GetCharacterState("dad") ~= "cleaning" and 
			GetCharacterState("dad") ~= "sit" and 
			GetCharacterState("dad") ~= "sit_outside" and 
			GetCharacterState("dad") ~= "sit_to_play" then
			i, j = string.find(self.dad_current_state, "holding");
			if i == 1 then
				PlayCharacterAnimation("dad", "standing_with_stuff");
				ShowCharacterStuff("dad", GetCharacterStuff("dad"), "0", "0", "0");
			else		
				PlayCharacterAnimation("dad", "idle");
				SetCharacterState("dad", "normal");
			end		
		end
		self.dad_coming = false;
	end
	
	if self.mom_coming then
		if GetCharacterState("mom") ~= "cleaning" and 
			GetCharacterState("mom") ~= "sit" and 
			GetCharacterState("mom") ~= "sit_outside" and 
			GetCharacterState("mom") ~= "sit_to_play" then
			i, j = string.find(self.mom_current_state, "holding");
			if i == 1 then
				PlayCharacterAnimation("mom", "standing_with_stuff");
				ShowCharacterStuff("mom", GetCharacterStuff("mom"), "0", "0", "0");
			else		
				PlayCharacterAnimation("mom", "idle");
				SetCharacterState("mom", "normal");
			end		
		end
		self.mom_coming = false;
	end
	
	if self.no_stress_power then
		HideNoStressIcon(self.object_name);
	end
	
	self.no_stress_power = false;
	
	HideDadIcon(self.object_name);
	HideMomIcon(self.object_name);
	
	RemoveObjectFromClickedList(self.object_name);
	ChangeObjectState(self.object_name, self.previous_state);
end

function family_time:sit_character(character_name)
	SetCharacterState(character_name, "sit_to_play");
	HideCharacter(character_name);
	ChangeObjectState(character_name.."familytime", "idle");
	
	if character_name == "dad" then
		HideDadNextTaskIcon(self.object_name);
	end
	
	if character_name == "mom" then
		HideMomNextTaskIcon(self.object_name);
	end
end

function family_time:clean_update()
	local n = table.getn(self.child_members);	
	
	self.all_kids_sat = true;
	
	for i = 1 , n do
		if GetCharacterState(self.child_members[i]) ~= "sit_to_play" then
			self.all_kids_sat = false;			
		end
	end
	
	if self.all_kids_sat then
		ShowBreakSign("family");
		ShowDadNextTaskIcon(self.object_name);
		ShowMomNextTaskIcon(self.object_name);
		ChangeObjectState(self.object_name, "dirty");
	end
end

function family_time:click()
	if self.click_enable then
		self.parent_chosen = GetSelectedParent();
		local current_parent_coming = false;
		if self.parent_chosen == "dad" and self.dad_coming or
			self.parent_chosen == "mom" and self.mom_coming then
			current_parent_coming = true;
		end
		
		if GetCharacterState(self.parent_chosen) ~= "sit_to_play" and not current_parent_coming then
			self:parent_command();
			PlaySound("check", false);
			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);
				self.dad_coming = true;
				self.dad_current_state = GetCharacterState(self.parent_chosen);
				self.dad_current_stuff = GetCharacterStuff(self.parent_chosen);
			else	
				ShowMomIcon(self.object_name);
				self.mom_coming = true;
				self.mom_current_state = GetCharacterState(self.parent_chosen);
				self.mom_current_stuff = GetCharacterStuff(self.parent_chosen);
			end
			
			if self.object_is_dirty then
				PlayGlowAnimation(self.object_name, "dirty_click");
			end
			
			
			--self.click_enable = false;
			--self.parent_is_coming = true;
			self.previous_state = GetObjectState(self.object_name);
			IncludeObjectInClickedList(self.object_name);
		end
	end
end

function family_time:parent_command()
	--RemoveObjectMouseHandler(self.object_name);
	if self.parent_chosen == "dad" then
		AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.node_to_dad_go);
	else
		AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", self.node_to_mom_go);
	end
end

function family_time:parent_action(character_name)
	local n = table.getn(self.child_members);
	
	if character_name == "dad" then
		HideDadIcon(self.object_name);
		self.dad_coming = false;
	else
		HideMomIcon(self.object_name);
		self.mom_coming = false;
	end
	if self.all_kids_sat then
		if GetCharacterState(character_name) == self.parent_state_action and self.object_is_dirty then
			self:sit_character(character_name);
			if GetCharacterState("dad") == "sit_to_play" and GetCharacterState("mom") == "sit_to_play" then
				for i = 1 , n do
					ChangeObjectState(self.child_members[i].."familytime", "playing");
				end
				
				ChangeObjectState("momfamilytime", "playing");
				ChangeObjectState("dadfamilytime", "playing");
				ChangeObjectState(self.object_name, self.cleaning_state_name);
				self.new_state_start_time = GetCurrentTime();
				RemoveObjectMouseHandler(self.object_name);
				
				RemoveObjectFromClickedList(self.object_name);
				
				StartResetStressMeter(self.cleaning_state_time / 1000.0);
			else
				IncludeObjectMouseHandler(self.object_name);
				self.click_enable = true;
			end
		else
			ShowMiss(self.object_name);
			if self.no_stress_power then
				HideNoStressIcon(self.object_name);
			end
			self.no_stress_power = false;
			--IncludeObjectMouseHandler(self.object_name);
			FinishCharacterTask(character_name);
			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
	else
		ShowMiss(self.object_name);
		if self.no_stress_power then
			HideNoStressIcon(self.object_name);
		end
		self.no_stress_power = false;
		IncludeObjectMouseHandler(self.object_name);
		FinishCharacterTask(character_name);
		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	
end

function family_time:cleaning_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		RemoveObjectMouseHandler(self.object_name);
		local n = table.getn(self.child_members);
		
		for i = 1 , n do
			ChangeObjectState(self.child_members[i].."familytime", "clean");
			ShowCharacter(self.child_members[i]);
			SetCharacterState(self.child_members[i], "normal");
			FinishCharacterTask(self.child_members[i]);
		end
		
		ChangeObjectState("momfamilytime", "clean");
		ChangeObjectState("dadfamilytime", "clean");
		
		ShowCharacter("dad");
		ShowCharacter("mom");
		
		SetCharacterState("dad", "normal");
		SetCharacterState("mom", "normal");
				
		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;
		
		PlayGlowAnimation(self.object_name, "clean");
		
		self.click_enable = false;		
		
		self.all_kids_sat = false;
		self.all_family_together = false;
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
		
		ChangeObjectState(self.object_name, self.clean_state_name);
		
		FinishCharacterTask("dad");
		FinishCharacterTask("mom");
		
		self.dad_coming = false;
		self.mom_coming = false;
		
		FinishTeamWorkTask("dad", "familytime", 1);
		FinishTeamWorkTask("mom", "familytime", 1);
	end
end

function family_time:clean_youngchild_action(character_name)
	self:sit_character(character_name);	
end

function family_time:clean_toddler_action(character_name)
	self:sit_character(character_name);
end

function family_time:clean_schoolaged_action(character_name)
	self:sit_character(character_name);
end
