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

-- discipline_kids_left_dirty.lua
-- discipline_kids_left_dirty table
discipline_kids_left_dirty = discipline_kids:new{
	character_messing_animation = "";
	object_left_dirty = "";
}

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

function discipline_kids_left_dirty:clean_mess_action()
	if self.object_to_verify == "" or GetObjectState(self.object_to_verify) ~= "cleaning" then
		PlayCharacterAnimation(self.current_action_character, self.character_messing_animation);
		FlipAnimation(self.current_action_character, self.flipped_animation);
		ChangeObjectState(self.object_name, self.dirty_state_name);
		
		if self.sound_of_messing ~= "" then
			PlaySound(self.sound_of_messing, true);
		end
		LoadCharacterTask(self.current_action_character, gotonodeanddo, self.node_of_discipline, self.object_name);
	else
		LoadCharacterTask(self.current_action_character, gotonodeanddo, self.node_of_discipline, "idle");
		FinishCharacterTask(self.current_action_character);
	end
end

function discipline_kids_left_dirty:dirty_init()
	DebugOut(""..self.object_name..":dirty_init");
	self.object_is_dirty = true;
	PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
	PlayObjectAnimation(self.object_left_dirty, self.object_dirty_animation);
	ChangeObjectState(self.object_left_dirty, "dirty");
	
	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;
		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
	end
	
	if self.check_teamwork then
		ShowTeamWorkIcon(self.object_name, ShouldShowCharacterTeamWork(self.teamworktask_name));
	end
	
	if self.sound_of_messed ~= "" then
		PlaySound(self.sound_of_messed, false);
	end
	
	self.new_state_start_time = GetCurrentTime();
end

function discipline_kids_left_dirty:dirty_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.messing_state_time and self.click_enable then
		if self.sound_of_messing ~= "" then
			StopSound(self.sound_of_messing);
		end
		--ChangeObjectState(self.object_left_dirty, "dirty");
		FinishCharacterTask(self.current_action_character);
		
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		PlayGlowAnimation(self.object_name, "clean");		
		
		ChangeObjectState(self.object_name, "childgiveup");
	end
end	

function discipline_kids_left_dirty:parent_action()	
	self.parent_is_coming = false;
	DebugOut("discipline_kids:parent_action:"..self.object_name);
	
	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) == self.parent_state_action and self.object_is_dirty then
		if self.sound_of_work_enable then
			if self.parent_chosen == "dad" then
				--PlaySound("working_father", true);
			else
				--PlaySound("working_mother", true);
			end
		end
		PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
		
		--ChangeObjectState(self.object_left_dirty, "dirty");
		FinishCharacterTask(self.current_action_character);
		
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		PlayGlowAnimation(self.object_name, "clean");		
		
		ChangeObjectState(self.object_name, "waitingchild");
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		
		if self.cleaning_timer_enabled then
			SetCleaningTimerVisible(self.object_name, true);
		end
		
		if self.sound_of_messing ~= "" then
			StopSound(self.sound_of_messing);
		end
		
		if self.sound_of_cleaning ~= "" then
			PlaySound(self.sound_of_cleaning, true);
		end
	else
		ShowMiss(self.object_name);
		if self.stress_inc_enabled and not self.no_stress_power then
			AddFutureCharacterStress(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);
		self.parent_chosen = "";
		self.click_enable = self.set_click_enable;
		if self.object_is_dirty then
			IncludeObjectMouseHandler(self.object_name);			
			self.click_enable = true;
			PlayObjectAnimation(self.object_name, self.object_dirty_animation);
		else
			if self.click_enable then
				IncludeObjectMouseHandler(self.object_name);
			end
		end
	end
end