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

local ObjectTable = GetObjectTable("studytime");

study_desk = simple_object:new{
	current_action_character="toddler",
	character_cleaning_animation = "doing_stuff_up",
	parent_state_action = "holding_books",	
	anchor_x = 0,
	anchor_y = 0,
	offset_x=-5,
	offset_y=-5,
	
	schoolaged_sit = false;
	
	cleaning_state_time = 10000;
	
	show_cleaning_particle = false;
	
	complex_object = true;
	dad_next_task_x = -20,
	dad_next_task_y = -30,
	mom_next_task_x = -20,
	mom_next_task_y = -30,
	
	stress_inc_enabled = false;
	
	cleaning_timer_x = 0;
	cleaning_timer_y = -100;
	
	dad_ui_x=-20,
	dad_ui_y=-60,
	mom_ui_x=-20,
	mom_ui_y=-60,

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

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

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

function study_desk:messing_schoolaged_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function study_desk:dirty_schoolaged_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function study_desk:flaging_schoolaged_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function study_desk:cleaning_schoolaged_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function study_desk:waitingchild_schoolaged_action(character_name)
	self.current_action_character = character_name;
	self:clean_mess_action();
end

function study_desk:clean_mess_action()
	ChangeObjectState(self.current_action_character.."studying", "idle");
	HideCharacter(self.current_action_character);
	self.schoolaged_sit = true;
end

function study_desk:parent_action()
	self.parent_is_coming = false;
	if self.parent_chosen == "dad" then
		HideDadIcon(self.object_name);
	else
		HideMomIcon(self.object_name);
	end
	DebugOut("study_desk:parent_action");
	RemoveObjectFromClickedList(self.object_name);
	if GetCharacterState(self.parent_chosen) == self.parent_state_action and self.object_is_dirty then
		DebugOut("study_desk:parent_action:do the action");
		if self.parent_chosen == "dad" then
			--PlaySound("working_father", true);
			HideDadNextTaskIcon(self.object_name);
		else
			--PlaySound("working_mother", true);
			HideMomNextTaskIcon(self.object_name);
		end
		HideCharacterStuff(self.parent_chosen);
		PlayCharacterAnimation(self.parent_chosen, self.character_clean_animation);
		SetCharacterState(self.parent_chosen, "normal");
		--AddCharacterStress(self.parent_chosen, self.stress_inc);
		FinishCharacterTask(self.parent_chosen);		
		
		PlayGlowAnimation(self.object_name, "clean");
		
		ChangeObjectState("materialestudo", "ontable");
		PlayObjectAnimation(self.object_name, self.object_cleaning_animation);
		ChangeObjectState(self.object_name, "waitingchild");
		self.new_state_start_time = GetCurrentTime();
		
		self:get_cleaning_state_time();		
		
		self.object_is_dirty = false;
	else
		DebugOut("study_desk:parent_action:miss!");
		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(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");
		end
	end
end

function study_desk:waitingchild_update()
	if self.schoolaged_sit then
		SetCleaningTimerVisible(self.object_name, true);
		ChangeObjectState(self.object_name, self.cleaning_state_name);
		ChangeObjectState(self.current_action_character.."studying", "playing");
	end
end

function study_desk:cleaning_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
		SetCleaningTimerVisible(self.object_name, false);
		if self.parent_chosen == "dad" then
			StopSound("working_father");
			PlaySound("task_complete_father", false);
		else
			StopSound("working_mother");
			PlaySound("task_complete_mother", false);
		end
		PlayObjectAnimation(self.object_name, self.object_clean_animation);
		ChangeObjectState(self.object_name, self.clean_state_name);
		
		ShowCharacter(self.current_action_character);
		FinishCharacterTask(self.current_action_character);
		
		ChangeObjectState("materialestudo", "dirty");
		
		ChangeObjectState(self.current_action_character.."studying", "clean");
		
		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;
				
		ShowNextTaskIcon("materialestudo");
				
		self.cleaning_state_time = self.previous_cleaning_state_time;		
		
		FinishTeamWorkTask("kids", "studytime", 1);
		
		self.schoolaged_sit = false;
	end
end