--[[⠀
----------------------------------------------------------------------------------------------------------------------------⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀                       ⠀⢀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣀⣀⣀⣀⣰⣦⣀⣀⠀⠠⠄⠄⠠⠀⠀⢀⣠⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣶⡿⢿⣦⠀⠀⠀⠀⠀⠀⠀⢀⡷⠀⠀⠀⠀⠀⣀⠀⠀⠀⠀⠀⣩⡿⠋⠙⠉⢩⡿⠟⠀⠀⠀⠀⠀⠀⢀⣶⣾⠟⠋⠛⣿⣷⡄⠀⠀⠀⠀⠀⣴⣿⠂⠀⠀⣼⡿⠀⠀⠀⠀⠀⠀⢀⣠⡴⠶⣤⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⠦⠤⣤⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⡿⠋⠀⢸⠏⠀⠀⠀⠀⠀⠀⠀⣼⠃⠀⠀⠀⠀⢠⡇⠀⠀⠀⢠⣾⠋⠀⠀⠀⠀⣸⠀⠀⠀⠀⠀⠀⠀⢠⣿⠋⠀⠀⠀⠀⠛⠛⠁⠀⠀⠀⢀⣾⣿⠏⠀⠀⢀⡿⠃⠀⠀⠀⠀⠀⠶⠋⠁⢀⣰⠟⠀⠀⠀⠀⠀⠀⠀⣹⣿⠀⠀⠀⠀⣨⣿⠇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣾⡟⢀⣠⣤⣴⣶⠆⠀⠀⠀⠀⠀⢰⠃⠀⠀⠀⠀⠀⣾⠀⠀⠀⣰⣿⠃⠀⠀⠀⠀⠀⣿⡀⠀⠀⠀⠀⠀⢀⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣿⠋⠀⠀⢀⡾⠁⠀⠀⠀⠀⠀⠀⠀⣠⣶⣯⣅⡀⠀⠀⠀⠀⠀⠀⠀⣿⡇⢀⣠⠴⠞⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⡟⠀⠛⠉⣠⣾⠃⠀⠀⠀⠀⠀⠀⣼⠀⠀⠀⠀⠀⠀⢿⣤⡴⢺⢻⠃⠀⠀⠀⠀⠀⢸⡿⠁⠀⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⢀⣤⠀⠀⠀⠀⠀⢀⣿⣇⡤⠤⢤⣾⣤⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢻⡆⠀⠀⠀⠀⠀⣿⣯⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⠃⠀⠀⢀⣼⠇⠀⠀⠀⠀⠀⠀⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢂⠇⠀⠀⠀⠀⠀⠀⣾⡅⠀⠀⠀⠀⠀⠀⠀⢸⣧⡀⠀⠀⣼⠇⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠐⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⢰⡏⠘⣦⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠘⢧⣤⡶⠟⢻⠀⠀⠀⠀⠀⠀⢠⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢘⠏⠀⠀⠀⠀⠀⠀⢰⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣶⡾⠋⠀⠀⠀⠀⠀⠀⢸⡟⠀⠀⠀⢰⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠞⠁⠀⠀⠀⠀⠀⠀⠁⠀⠈⠙⠂⠔⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠀⠀⠀⠀⢀⣿⣤⠶⠒⠒⠁⠀⠀⠀⠀⢠⠏⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠐⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

----------------------------------------------------------------------------------------------------------------------------
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Please contact me if you need to hire someone to do mods or other design related tasks
https://steamcommunity.com/id/glytch3r/myworkshopfiles/
https://www.glytch3r.com
https://ko-fi.com/glytch3r
Discord: Glytch3r#2892

----------------------------------------------------------------------------------------------------------------------------
--]]
require "recipecode"


Recipe = Recipe or {}
Recipe.GetItemTypes = Recipe.GetItemTypes or {}
Recipe.OnCanPerform = Recipe.OnCanPerform or {}
Recipe.OnCreate = Recipe.OnCreate or {}
Recipe.OnGiveXP = Recipe.OnGiveXP or {}
Recipe.OnTest = Recipe.OnTest or {}

local function dakiBuffEffect()
	
	local item = nil
	local player = getPlayer()

	--just type the Profession on the "" and remove the -- on the left side of the line below
	if player then
    	local descriptor = player:getDescriptor()
   	 	if descriptor then
        	local profession = descriptor:getProfession()
        	if profession then
            	local professionIsWeeb = profession == "Weeb"
            	if not professionIsWeeb then return end
        	else
            	return
        	end
    	else
        	return
    	end
	else
    	return
	end

	
	if player:getInventory():getFirstEvalRecurse(function(item)			
			return item:hasTag("DakiWeapon") or item:hasTag("DakiArmorFront") or item:hasTag("DakiArmorBack")
		end) then
		--player:getModData()['BuffEffect'] = "daki found" -- for testing
		local bodyDamage = player:getBodyDamage()
		local stats = player:getStats()

		bodyDamage:setUnhappynessLevel(bodyDamage:getUnhappynessLevel() - (bodyDamage:getUnhappynessLevel() / 4))
		if bodyDamage:getUnhappynessLevel() < 0 then
			bodyDamage:setUnhappynessLevel(0);
		end
		if player:isAsleep() then
			stats:setStress(stats:getStress() - (stats:getStress() / 4))
			if stats:getStress() < 0 then
				stats:setStress(0);
			end
		end
		-- else  player:getModData()['BuffEffect'] = "something might be wrong" -- for testing
	end
	--player:Say(player:getModData()['BuffEffect']) -- for testing
end

Events.EveryTenMinutes.Add(dakiBuffEffect)

local bodyParts = {
	BloodBodyPartType.Back,
	BloodBodyPartType.Foot_L,
	BloodBodyPartType.Foot_R,
	BloodBodyPartType.ForeArm_L,
	BloodBodyPartType.ForeArm_R,
	BloodBodyPartType.Groin,
	BloodBodyPartType.Hand_L,
	BloodBodyPartType.Hand_R,
	BloodBodyPartType.Head,
	BloodBodyPartType.LowerLeg_L,
	BloodBodyPartType.LowerLeg_R,
	BloodBodyPartType.Neck,
	BloodBodyPartType.Torso_Lower,
	BloodBodyPartType.Torso_Upper,
	BloodBodyPartType.UpperArm_L,
	BloodBodyPartType.UpperArm_R,
	BloodBodyPartType.UpperLeg_L,
	BloodBodyPartType.UpperLeg_R
}

local function transferData(old,new)
	new:copyModData(old:getModData())
end

local function dataFromClothing(clothingItem,result)
	local clothing = result:getModData()

	local visual = clothingItem:getVisual()
	for i , v in ipairs(bodyParts) do
		local part = {}
		part.dirt = visual:getDirt(v)
		part.blood = visual:getBlood(v)
		part.hasHole = visual:getHole(v) ~= 0

		local patch = clothingItem:getPatchType(v)
		if patch then
			local tempTable = {}
			tempTable['biteDefense'] = patch:getBiteDefense()
			tempTable['fabricTypeName'] = patch:getFabricTypeName()
			tempTable['scratchDefense'] = patch:getScratchDefense()
			part.patch = tempTable
		else
			part.patch = nil
		end

		clothing[i] = part
	end

	clothing.dirtyness = clothingItem:getDirtyness()
	--clothing.bloodLevel = clothingItem:getBloodLevel()
end

local function dataToClothing(clothingItem, result, player)
	local clothing = clothingItem:getModData()
	if type(clothing[1]) ~= "table" then return end

	local visual = result:getVisual()
	for i , v in ipairs(bodyParts) do
		local part = clothing[i]
		visual:setDirt(v, part.dirt)
		visual:setBlood(v, part.blood)
		if part.hasHole then visual:setHole(v) end

		if part.patch then
			local fullType = 'Base.' .. string.gsub(part.patch.fabricTypeName, '%s+', '')
			local fabricItem = instanceItem(fullType)
			result:addPatch(player, v, fabricItem)
		end

		clothing[i] = nil
	end
	result:setDirtyness(clothing.dirtyness)
	--result:setBloodLevel(clothing.bloodLevel)
end


function Recipe.OnCreate.dakiCondition(result,selectedItem, player)

local function isType(item)
	if string.find(item:getType(),"Armor") then
		return "armor"
	else return "daki"
	end
	return nil
end
if not(isType(result) or isType(selectedItem)) then print(" proper items not found for recipe") return end

	if isType(selectedItem) == "armor" and isType(result) ==  "daki" then
		dataFromClothing(selectedItem,result)
		result:setBloodLevel(selectedItem:getBloodLevel()/100)
	end

	if isType(selectedItem) == "daki" and isType(result) ==  "armor" then
		dataToClothing(selectedItem,result,player)
		result:setBloodLevel(selectedItem:getBloodLevel() * 100)
	end
	------------------------   DBG         ---------------------------
	if isDebugEnabled() then 
		print('----------------------------------------------')
		if isType(result) == "daki" then 
			print('weapon mode') ; getPlayer():Say('weapon mode') 
		elseif isType(result) == "armor" then 
			print('armor mode') ; getPlayer():Say('armor mode')  
		end
		if  isType(result) == "armor" then	

			print('result:getDirtyness '..result:getDirtyness()) 
			print('result:getBloodLevel '..result:getBloodLevel()) 
		end
	
		result:setCondition(selectedItem:getCondition())
		print('result:getCondition '..result:getCondition())
		print('----------------------------------------------')
	else
	
	local condition = selectedItem:getCondition()
		result:setCondition(condition)
		result:resetModelNextFrame()
	end 
end


 function Recipe.OnCreate.MakeArmor(craftRecipeData, player)
	local daki = craftRecipeData:getAllConsumedItems():get(1)
	local name = daki:getFullType()
	if string.find(name,"Flip") then name = string.gsub(name,"Flip","")end
	print("armor input name = " .. name)
	local result = instanceItem(name.."_FrontArmor") 
	result:setCondition(daki:getCondition())
	Recipe.OnCreate.dakiCondition(player:getInventory():AddItem(result),daki,player)
	print(name.."_FrontArmor")
end
function Recipe.OnCreate.UndoArmor(craftRecipeData, player)
	local armor = craftRecipeData:getAllConsumedItems():get(0)
	local name = armor:getFullType()
	print("armor input name = " .. name)
	if string.find(name,"_FrontArmor")then
		name = string.gsub(name,"_FrontArmor","")
	else name = string.gsub(name,"_BackArmor","")end
	print("armor output name = " .. name)
	local result = instanceItem(name) 
	result:setCondition(armor:getCondition())
	Recipe.OnCreate.dakiCondition(player:getInventory():AddItem(result),armor, player)
	getPlayer():getInventory():AddItem("Base.Rope");
end

function Recipe.OnCreate.DakiFlip(craftRecipeData, player)
    local item = craftRecipeData:getAllConsumedItems():get(0)
    local name = item:getFullType()
    if string.find(name,"_Flip") then
        name = string.gsub(name,"_Flip","")
    elseif string.find(name,"Flip") then
        name = string.gsub(name,"Flip","")
    else
        name = name .. "Flip"
        if instanceItem(name) == nil then
            name = string.gsub(name,"Flip","")
            name = name .. "_Flip"
        end
    end
    local result = instanceItem(name)
    result:setCondition(item:getCondition())
    getPlayer():getInventory():AddItem(result)
    ISTimedActionQueue.add(ISEquipWeaponAction:new(player, result, 25, false, true))
    transferData(item,result)
end

