require( "scripts/objects/objects.lua" );

-- simple_object.lua
-- simple_object table
simple_object = {
	-- ANCHOR
	anchor_x=0,
	anchor_y=0,
	offset_x=0,
	offset_y=0,
	
	-- TEXTURE
	texture="",
	
	-- UI
	animated=true,
	loaduiicons=true,
	indicator_ui_x=0,
	indicator_ui_y=-90,
	dad_ui_x=-125,
	dad_ui_y=-90,
	mom_ui_x=-55,
	mom_ui_y=-90,
	nanny_ui_x=10,
	nanny_ui_y=10,
	
	-- INIT STATE
	init_state="clean",

	-- OBJECT
	object_name = "",
	object_clean_animation = "clean",
	object_messing_animation = "messing",
	object_dirty_animation = "dirty",
	object_dirty_mouse_on_animation = "dirty_mouse_on",
	object_cleaning_animation = "cleaning",

	-- STATES NAMES
	clean_state_name = "clean",
	messing_state_name = "messing",
	dirty_state_name = "dirty",
	dirty_mouse_on_state_name = "dirty_mouse_on",
	flaging_state_name = "flaging",
	flaged_state_name = "flaged",
	cleaning_state_name = "cleaning",

	-- CHARACTER
	current_action_character = "youngchild",
	character_messing_animation = "doing_stuff_sit_down",
	character_dirty_animation = "idle",
	character_cleaning_animation = "doing_bow_down",
	character_clean_animation = "idle",
	flipped_animation = false,

	-- PARENT
	parent_chosen = "",
	parent_state_action = "normal",

	-- TIME
	new_state_start_time = 0,
	messing_state_time = 1000,
	cleaning_state_time = 1000,
	selecting_state_time = 2000,
	parent_select_intial_time = 0,
	previous_cleaning_state_time = cleaning_state_time,
	
	-- CHAOS
	
	chaos_init_inc = 5;
	chaos_update_inc = 1;
	chaos_amount = 0;
	chaos_clean_inc = 12;
	chaos_state_timer = 0;
	
	
	-- STRESS
	stress_inc = 15;
	no_stress_power = false;
	stress_inc_enabled = true;
	
	-- MASK
	mask = false;
	maskX = 0;
	maskY = 0;
	maskWidth = 0;
	maskHeight = 0;

	mouse_on = false,
	dad_mouse_on = false,
	mom_mouse_on = false,	
	
	--mouse_over_enabled = true,	
	object_is_dirty = false;
	clicked = false;
	set_click_enable = true;
	click_enable = true;
	parent_is_coming = false;
	previous_state = "";
	
	check_teamwork = false;
	finish_teamwork = false;
	teamworktask_name = "";
	
	show_cleaning_particle = true;
	
	parent_current_state = "";
	parent_current_stuff = "";
	
	sound_of_work_enable = true;
	
	sound_of_messing = "";
	sound_of_messed = "";
	sound_of_cleaning = "";
	
	cleaning_timer_enabled = false;
	
	particle_x = "0";
	particle_y = "0";
	
	is_a_mess = true;
}

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

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

function simple_object:clean_mess_action()
	PlayCharacterAnimation(self.current_action_character, self.character_messing_animation);
	FlipAnimation(self.current_action_character, self.flipped_animation);
	PlayObjectAnimation(self.object_name, self.object_messing_animation);
	ChangeObjectState(self.object_name, self.messing_state_name);
	self.new_state_start_time = GetCurrentTime();
	if self.sound_of_messing ~= "" then
		PlaySound(self.sound_of_messing, true);
	end	
	self.previous_state = "messing";
end
	
function simple_object:messing_update()
	if (GetCurrentTime() - self.new_state_start_time) > self.messing_state_time then
		if self.sound_of_messing ~= "" then
			StopSound(self.sound_of_messing);
		end
		PlayCharacterAnimation(self.current_action_character, self.character_dirty_animation);
		FlipAnimation(self.current_action_character, self.flipped_animation);
		FinishCharacterTask(self.current_action_character);
		ChangeObjectState(self.object_name, self.dirty_state_name);
	end
end	

function simple_object:cancel()
	DebugOut(""..self.object_name..":cancel");	
	DebugOut(""..self.object_name..":previous:"..self.previous_state);	
	DebugOut(""..self.object_name..":parent:"..self.parent_chosen);	
	
	self.parent_is_coming = false;
	--self.click_enable = self.set_click_enable;
	--IncludeObjectMouseHandler(self.object_name);	
	
	if GetCharacterState(self.parent_chosen) ~= "cleaning" and
		GetCharacterState(self.parent_chosen) ~= "sit" and
		GetCharacterState(self.parent_chosen) ~= "sit_outside" and 
		GetCharacterState(self.parent_chosen) ~= "sit_to_play"	then
		i, j = string.find(GetCharacterState(self.parent_chosen), "holding");
		if i ~= 1 then
			PlayCharacterAnimation(self.parent_chosen, "idle");
			SetCharacterState(self.parent_chosen, "normal");
		else			
			PlayCharacterAnimation(self.parent_chosen, "standing_with_stuff");
			ShowCharacterStuff(self.parent_chosen, GetCharacterStuff(self.parent_chosen), "0", "0", "0");
		end		
	end
		
	if self.previous_state == "dirty" then
		if self.stress_inc_enabled and not self.no_stress_power then
			AddFutureCharacterStress(self.parent_chosen, -self.stress_inc);
		end
	end
	
	if self.no_stress_power then
		HideNoStressIcon(self.object_name);
	end
	
	self.no_stress_power = false;
	
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	
	RemoveObjectFromClickedList(self.object_name);
	if self.previous_state ~= "messing" then
		ChangeObjectState(self.object_name, self.previous_state);
	end
	--self.parent_chosen = "";
end

function simple_object:dirty_init()
	DebugOut(""..self.object_name..":dirty_init");
	self.object_is_dirty = true;
	PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
	--PlayGlowAnimation(self.object_name, "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));
		--ShowTeamWorkIcon(self.object_name, "mom", ShouldShowCharacterTeamWork("mom", self.teamworktask_name));
	end
	
	if self.sound_of_messed ~= "" then
		PlaySound(self.sound_of_messed, false);
	end
end

function simple_object:cleaning_update()
		if self.cleaning_timer_enabled then
			SetCleaningTimerProgress(self.object_name, (GetCurrentTime() - self.new_state_start_time) / self.cleaning_state_time);
		end
		if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
			if self.sound_of_cleaning ~= "" then
				StopSound(self.sound_of_cleaning);
			end
			if self.sound_of_messed ~= "" then
				StopSound(self.sound_of_messed);
			end
			
			if self.cleaning_timer_enabled then
				SetCleaningTimerVisible(self.object_name, false);
			end
			
			if self.parent_chosen == "dad" then
				if self.sound_of_work_enable then
					StopSound("working_father");
				end
				PlaySound("task_complete_father", false);
			else
				if self.sound_of_work_enable then
					StopSound("working_mother");
				end
				PlaySound("task_complete_mother", false);
			end
			
			PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
			FlipAnimation(self.parent_chosen, self.flipped_animation);
			--HideParticle(self.object_name, "powder");
			HideParticle(self.object_name, "powder");
			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");
			
			if self.finish_teamwork then
				FinishTeamWorkTask(self.parent_chosen, self.teamworktask_name, 1);
			end
			
			if self.check_teamwork then
				ShowTeamWorkIcon(self.object_name, 2);
				--ShowTeamWorkIcon(self.object_name, "mom", false);		
			end
			
			if self.is_a_mess then
				FinishTeamWorkTask(self.parent_chosen, "messes", 1);
			end
			
			self.cleaning_state_time = self.previous_cleaning_state_time;
		end
	--end
end

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

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

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

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

function simple_object:mousein()
	PlayGlowAnimation(self.object_name, "dirty");
end

function simple_object:mouseout()
	PlayGlowAnimation(self.object_name, "clean");
end

function simple_object:click()
	DebugOut(""..self.object_name..":click");	
	if self.click_enable then		
		DebugOut(""..self.object_name..":click_enable");
		--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	
		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 simple_object:icon_timer_update()
	if self.clicked then
		if (GetCurrentTime() - self.parent_select_intial_time) > self.selecting_state_time then
			self:parent_command();
			if self.parent_chosen == "dad" then
				SetAlphaDadIcon(self.object_name, 100);
			else
				SetAlphaMomIcon(self.object_name, 100);
			end
			self.clicked = false;			
		end
	end
end

function simple_object:parent_command()
	DebugOut("simple_object:parent_command:"..self.object_name);
	RemoveObjectMouseHandler(self.object_name);
	AddCharacterTask(self.parent_chosen, self.object_name, "gotoanddo", "");
end

function simple_object:get_cleaning_state_time()
	if IsNannyCleaningPowerActive() then
		self.previous_cleaning_state_time = self.cleaning_state_time;
		self.cleaning_state_time = self.cleaning_state_time / 10;
	end
end

function simple_object:parent_action()	
	self.parent_is_coming = false;
	DebugOut("simple_object: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
		PlayCharacterAnimation(self.parent_chosen, self.character_cleaning_animation);
		FlipAnimation(self.parent_chosen, self.flipped_animation);
		PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
		
		if self.show_cleaning_particle then
			--ShowParticle(self.object_name, "effects/powder.lua", "0", "0"););
			ShowParticle(self.object_name, "effects/powder.lua", self.particle_x, self.particle_y);
		end
		
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		self.new_state_start_time = GetCurrentTime();
		self:get_cleaning_state_time();
		
		if self.cleaning_timer_enabled then
			SetCleaningTimerVisible(self.object_name, true);
		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);	
			--PlayGlowAnimation(self.object_name, "dirty");
		else
			if self.click_enable then
				IncludeObjectMouseHandler(self.object_name);
			end
		end
	end
end

function simple_object:cleanpower_update()
	if not self.complex_object and not self.parent_is_coming then
		ChangeObjectState(self.object_name, "powercleaning");
		self.new_state_start_time = GetCurrentTime();
		--SetCleaningTimerVisible(self.object_name, true);
		self.click_enable = false;
		PlayGlowAnimation(self.object_name, "dirty_click");
		ShowParticle(self.object_name, "effects/auto_task.lua", 0, 0);
	else
		ChangeObjectState(self.object_name, "dirty");
	end
end


function simple_object:powercleaning_update()
	--SetCleaningTimerProgress(self.object_name, (GetCurrentTime() - self.new_state_start_time) / self.cleaning_state_time);
	if (GetCurrentTime() - self.new_state_start_time) > self.cleaning_state_time then
		if self.sound_of_cleaning ~= "" then
			StopSound(self.sound_of_cleaning);
		end
		if self.sound_of_messed ~= "" then
			StopSound(self.sound_of_messed);
		end
		SetCleaningTimerVisible(self.object_name, false);
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
				
		HideParticle(self.object_name, "powder");
	end
end