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

local ObjectTable = GetObjectTable("walldrawing");

wall_drawing = simple_object:new{
	current_action_character="youngchild",
	anchor_x=-30,
	anchor_y=40,
	offset_x=5,
	offset_y=15,
	texture="anim/objects/wall_drawing/wall_drawing",
	animation_script="anim/objects/wall_drawing/drawing_animation_script.lua",
	glow_texture="anim/objects/wall_drawing/wall_drawing_glow",
	glow_animation_script="anim/objects/default_glow_animation_script.lua",
	character_messing_animation = "doing_stuff_up",
	character_cleaning_animation = "doing_stuff_up",	
	
	flip_animation_action_character = false,
	flip_animation_parent = false,
	
	pano_x = "-50";
	pano_y = "-80";
	pano_layer = "-1";
	
	messing_state_time = 2000,
	cleaning_state_time = 3500,
	
	set_click_enable = false;
	
	mask = true;
	maskX = 5;
	maskY = 0;
	maskWidth = 70;
	maskHeight = 90;
	maskAlpha = 0.01;
	
	sound_of_messing = "drawing";

	messing_state_time = ObjectTable["messing_time"],
	cleaning_state_time = ObjectTable["cleaning_time"],
	stress_inc = ObjectTable["stress_inc"],
	
	cancelled = false;
}

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

function wall_drawing: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
	
	self.cancelled = true;
	
	RemoveObjectFromClickedList(self.object_name);
	if self.previous_state ~= "messing" then
		ChangeObjectState(self.object_name, self.previous_state);
	end
	--self.parent_chosen = "";
end

function wall_drawing:dirty_init()
	DebugOut(""..self.object_name..":dirty_init");
	self.object_is_dirty = true;
	if not self.cancelled then
		PlayObjectAnimation(self.object_name, self.object_dirty_animation);	
	else
		self.cancelled = false;
	end
	--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));
	end
	
	if self.sound_of_messed ~= "" then
		PlaySound(self.sound_of_messed, false);
	end
end

function wall_drawing:clean_mess_action()	
	PlayCharacterAnimation(self.current_action_character, self.character_messing_animation);
	FlipAnimation(self.current_action_character, self.flip_animation_action_character);
	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 wall_drawing: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) == self.parent_state_action and self.object_is_dirty then
		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, "pano", self.pano_x, self.pano_y, self.pano_layer);
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		self.new_state_start_time = GetCurrentTime();
		ShowParticle(self.object_name, "effects/powder.lua", "0", "0");
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		
		self:get_cleaning_state_time();
	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;
		--PlayObjectAnimation(self.object_name, self.object_dirty_animation);
		--ChangeObjectState(self.object_name, self.dirty_state_name);
		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
end

function wall_drawing: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);
		HideParticle(self.object_name, "powder");
		SetCharacterState(self.parent_chosen, self.cleaning_state_name);
		SetCharacterState(self.parent_chosen, "normal");
		
		self.cleaning_state_time = self.previous_cleaning_state_time;
		
		FinishTeamWorkTask(self.parent_chosen, "messes", 1);
	end
end