require "TimedActions/ISBaseTimedAction"

LabActionMakeAutopsy = ISBaseTimedAction:derive("LabActionMakeAutopsy");

function LabActionMakeAutopsy:isValid()
	return true;	-- была только эта строка
end

function ISPaintAction:waitToStart()
	self.character:faceThisObject(self.corpse or self.bottom);
	return self.character:shouldBeTurning();
end

function LabActionMakeAutopsy:update()
	self.character:faceThisObject(self.corpse or self.bottom);
	self.character:setMetabolicTarget(Metabolics.MediumWork);
end

function LabActionMakeAutopsy:start()
	self:setActionAnim("Loot");
	self.character:SetVariable("LootPosition", not(self.corpse) and "Mid" or "Low");
	self.SoundZREmeatA = self.character:playSound("zReVAC2_Meat_A");
	
	if self.corpse then
        self.corpse:getModData().Autopsy = true;	-- fix by Yummy
		self.corpse:transmitModData();
    end
end

function LabActionMakeAutopsy:stop()
	if self.corpse then
        self.corpse:getModData().Autopsy = false;	-- fix by Yummy
		self.corpse:transmitModData();
    end
	self.character:stopOrTriggerSound(self.SoundZREmeatA);
	
	ISBaseTimedAction.stop(self);
end

function LabActionMakeAutopsy:perform()	
    self.character:removeFromHands(self.character:getSecondaryHandItem());
	
	local tool = self.character:getPrimaryHandItem();	-- addon by Yummy
	if tool:getBloodLevel() < 1 then					--
        tool:setBloodLevel(1);							--
    end													--
	
	local inv = self.character:getInventory();
	
	--- pizdec randomizer by kerhus v4.44 ---
	local pizdec = ZombRand(1, 101); -- базовый выброс кубика
	local pizdecSBRare = SandboxVars.zReV2.chanceToRare;
	local pizdecSBNormal = SandboxVars.zReV2.chanceToNormal + pizdecSBRare;
	if pizdecSBRare >= pizdec then 							-- 
			local pizdecRareVariables = ZombRand(1, 101);
			if 5 >= pizdecRareVariables then
				inv:AddItem("LabItems.MatPlagueSamplesRare", 2);
			else
				inv:AddItem("LabItems.MatPlagueSamplesRare", 1);
			end
		elseif pizdecSBNormal >= pizdec then				--
			local pizdecNormalVariables = ZombRand(1, 101);
			if 5 >= pizdecNormalVariables then
				inv:AddItem("LabItems.MatPlagueSamplesNormal", 3);
			elseif 15 >= pizdecNormalVariables then
				inv:AddItem("LabItems.MatPlagueSamplesNormal", 2);
			else
				inv:AddItem("LabItems.MatPlagueSamplesNormal", 1);
			end
		else												--
			local SayNOTRESULT = "IGUI_zReVaccineNotResult"
			local SayNRVARIANT = ""
			local SayNRTOTAL = ""
			SayNRVARIANT = ZombRand(1, 4)
			SayNRTOTAL = tostring(SayNOTRESULT.."_"..SayNRVARIANT)
			self.character:Say(getText(SayNRTOTAL));
	end
	-----------------------------------
	self.character:getXp():AddXP(Perks.Doctor, ZombRand(2, 5));
	if self.character:HasTrait("Hemophobic") then
		self.character:getStats():setPanic(self.character:getStats():getPanic()+ZombRand(8, 15));
	end
	addBloodSplat(self.character:getCurrentSquare(), ZombRand(10));
	
	self.character:stopOrTriggerSound(self.SoundZREmeatA);
	
	ISBaseTimedAction.perform(self);
end

function LabActionMakeAutopsy:new(character, corpse, square, bottom)
	local o = {};
	setmetatable(o, self);
	self.__index = self;
	o.character = character;
	o.corpse = corpse;
	o.square = square;
	o.bottom = bottom;
	o.isDoctor = character:getDescriptor():getProfession() == "doctor";
	o.maxTime = 250-(character:getPerkLevel(Perks.Doctor)*10)-(o.isDoctor and 50 or 0);
	o.stopOnWalk = true;
	o.stopOnRun = true;
	return o;
end
