-- This file is part of FS25_Through_The_Years.
--
-- FS25_Through_The_Years is free software: you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- FS25_Through_The_Years is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
-- details.
--
-- You should have received a copy of the GNU General Public License along with
-- FS25_Through_The_Years. If not, see <https://www.gnu.org/licenses/>.

TypeControlOptions = {
}

-- FS25 option states start at 1 and not 0, so adapt to that by starting at 1.
TypeControlOptions.STATE_ENABLED = 1
TypeControlOptions.STATE_ENABLED_NO_MAX = 2
TypeControlOptions.STATE_MIN_ONE = 3
TypeControlOptions.STATE_DISABLED = 4

function TypeControlOptions.getDefaultOptions()
	return {
		ANIMALTRANSPORT        = { name = "Animal Transport", state = TypeControlOptions.STATE_ENABLED },
		AUGERWAGONS            = { name = "Auger Wagons", state = TypeControlOptions.STATE_ENABLED },
		BALELOADERS            = { name = "Bale Loaders", state = TypeControlOptions.STATE_ENABLED },
		BALERS                 = { name = "Balers", state = TypeControlOptions.STATE_ENABLED },
		BALEWRAPPERS           = { name = "Bale Wrappers", state = TypeControlOptions.STATE_ENABLED },
		BALINGMISC             = { name = "Baling Miscellaneous", state = TypeControlOptions.STATE_ENABLED },
		BARRELS                = { name = "Barrels", state = TypeControlOptions.STATE_ENABLED },
		BEETHARVESTERS         = { name = "Beet Harvester", state = TypeControlOptions.STATE_ENABLED },
		BEETHARVESTERCUTTERS   = { name = "Beet Harvester Cutters", state = TypeControlOptions.STATE_ENABLED },
		BEETLOADING            = { name = "Beet Loading", state = TypeControlOptions.STATE_ENABLED },
		BELTS                  = { name = "Belts", state = TypeControlOptions.STATE_ENABLED },
		CARS                   = { name = "Cars", state = TypeControlOptions.STATE_ENABLED },
		CHAINSAWS              = { name = "Chainsaws", state = TypeControlOptions.STATE_DISABLED },
		COMBINEWINDROWER       = { name = "Combine Windrower", state = TypeControlOptions.STATE_ENABLED },
		CORNHEADERS            = { name = "Corn Headers", state = TypeControlOptions.STATE_ENABLED },
		COTTONHARVESTERS       = { name = "Cotton Harvesters", state = TypeControlOptions.STATE_ENABLED },
		COTTONTRANSPORT        = { name = "Cotton Transport", state = TypeControlOptions.STATE_ENABLED },
		CULTIVATORS            = { name = "Cultivators", state = TypeControlOptions.STATE_ENABLED },
		CUTTERTRAILERS         = { name = "Cutter Trailers", state = TypeControlOptions.STATE_ENABLED },
		CUTTERS                = { name = "Cutters", state = TypeControlOptions.STATE_ENABLED },
		DISCHARROWS            = { name = "Disc Harrows", state = TypeControlOptions.STATE_ENABLED },
		FERTILIZERSPREADERS    = { name = "Fertilizer Spreaders", state = TypeControlOptions.STATE_ENABLED },
		FISHINGRODS            = { name = "Fishing Rods", state = TypeControlOptions.STATE_DISABLED },
		FLASHLIGHTS            = { name = "Flashlights", state = TypeControlOptions.STATE_DISABLED },
		FORAGEHARVESTERCUTTER  = { name = "Forage Harvest Cutters", state = TypeControlOptions.STATE_ENABLED },
		FORAGEHARVESTERS       = { name = "Forage Harvesters", state = TypeControlOptions.STATE_ENABLED },
		FORAGEMIXERS           = { name = "Forage Mixers", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYEXCAVATORTOOLS = { name = "Forestry Excavator Tools", state = TypeControlOptions.STATE_DISABLED },
		FORESTRYEXCAVATORS     = { name = "Forestry Excavators", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYFORWARDERS     = { name = "Forestry Forwarders", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYHARVESTERS     = { name = "Forestry Harvesters", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYMISC           = { name = "Forestry Miscellaneous", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYMULCHERS       = { name = "Forestry Mulchers", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYPLANTERS       = { name = "Forestry Planters", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYSTUMPCUTTERS   = { name = "Forestry Stump Cutters", state = TypeControlOptions.STATE_ENABLED },
		FORESTRYWINCHES        = { name = "Forestry Winches", state = TypeControlOptions.STATE_ENABLED },
		FORKLIFTS              = { name = "Forklifts", state = TypeControlOptions.STATE_ENABLED },
		FRONTLOADERTOOLS       = { name = "Frontloader Tools", state = TypeControlOptions.STATE_DISABLED },
		FRONTLOADERVEHICLES    = { name = "Frontloader Vehicles", state = TypeControlOptions.STATE_ENABLED },
		FRONTLOADERS           = { name = "Frontloaders", state = TypeControlOptions.STATE_DISABLED },
		GRAPEHARVESTERS        = { name = "Grape Harvesters", state = TypeControlOptions.STATE_ENABLED },
		GRAPETOOLS             = { name = "Grape Tools", state = TypeControlOptions.STATE_ENABLED },
		GRAPETRAILERS          = { name = "Grape Trailers", state = TypeControlOptions.STATE_ENABLED },
		GRASSLANDCARE          = { name = "Grassland Care", state = TypeControlOptions.STATE_ENABLED },
		GREENBEANHARVESTERS    = { name = "Green Bean Harvesters", state = TypeControlOptions.STATE_ENABLED },
		HANDTOOLSANIMALS       = { name = "Hand Tools Animals", state = TypeControlOptions.STATE_DISABLED },
		HANDTOOLSMISC          = { name = "Hand Tools Miscellaneous", state = TypeControlOptions.STATE_DISABLED },
		HARVESTERS             = { name = "Harvesters", state = TypeControlOptions.STATE_ENABLED },
		LEVELER                = { name = "Levelers", state = TypeControlOptions.STATE_ENABLED },
		LOADERWAGONS           = { name = "Loader Wagons", state = TypeControlOptions.STATE_ENABLED },
		LOWLOADERS             = { name = "Low Loaders", state = TypeControlOptions.STATE_ENABLED },
		MANURESPREADERS        = { name = "Manure Spreaders", state = TypeControlOptions.STATE_ENABLED },
		MARKINGSPRAY           = { name = "Marking Spray", state = TypeControlOptions.STATE_DISABLED },
		MISC                   = { name = "Miscellaneous Tools", state = TypeControlOptions.STATE_DISABLED },
		MISCDRIVABLES          = { name = "Miscellaneous Drivables", state = TypeControlOptions.STATE_ENABLED },
		MOWERS                 = { name = "Mowers", state = TypeControlOptions.STATE_ENABLED },
		MULCHERS               = { name = "Mulchers", state = TypeControlOptions.STATE_ENABLED },
		NEXAT                  = { name = "Nexat", state = TypeControlOptions.STATE_ENABLED },
		OLIVEHARVESTERS        = { name = "Olive Harvesters", state = TypeControlOptions.STATE_ENABLED },
		ONIONHARVESTING        = { name = "Onion Harvesters", state = TypeControlOptions.STATE_ENABLED },
		ONIONPLANTING          = { name = "Onion Planters", state = TypeControlOptions.STATE_ENABLED },
		PEAHARVESTERS          = { name = "Pea Harvesters", state = TypeControlOptions.STATE_ENABLED },
		PLANTERS               = { name = "Planters", state = TypeControlOptions.STATE_ENABLED },
		PLOWS                  = { name = "Plows", state = TypeControlOptions.STATE_ENABLED },
		POTATOHARVESTING       = { name = "Potato Harvesting", state = TypeControlOptions.STATE_ENABLED },
		POTATOPLANTING         = { name = "Potato Planting", state = TypeControlOptions.STATE_ENABLED },
		POWERHARROWS           = { name = "Power Harrows", state = TypeControlOptions.STATE_ENABLED },
		RICEHARVESTERS         = { name = "Rice Harvesters", state = TypeControlOptions.STATE_ENABLED },
		RICEPLANTERS           = { name = "Rice Planters", state = TypeControlOptions.STATE_ENABLED },
		ROLLERS                = { name = "Rollers", state = TypeControlOptions.STATE_ENABLED },
		SEEDTANKS              = { name = "Seed Tanks", state = TypeControlOptions.STATE_ENABLED },
		SEEDERS                = { name = "Seeders", state = TypeControlOptions.STATE_ENABLED },
		SILOCOMPACTION         = { name = "Silo Compactors", state = TypeControlOptions.STATE_ENABLED },
		SKIDSTEERTOOLS         = { name = "Skid Steer Tools", state = TypeControlOptions.STATE_DISABLED },
		SKIDSTEERVEHICLES      = { name = "Skid Steer Vehicles", state = TypeControlOptions.STATE_ENABLED },
		SLURRYTANKS            = { name = "Slurry Tanks", state = TypeControlOptions.STATE_ENABLED },
		SLURRYTOOLS            = { name = "Slurry Tools", state = TypeControlOptions.STATE_ENABLED },
		SLURRYTRANSPORT        = { name = "Slurry Transport", state = TypeControlOptions.STATE_ENABLED },
		SPADERS                = { name = "Spaders", state = TypeControlOptions.STATE_ENABLED },
		SPECIALHEADERS         = { name = "Special Headers", state = TypeControlOptions.STATE_ENABLED },
		SPINACHHARVESTERS      = { name = "Spinach Harvesters", state = TypeControlOptions.STATE_ENABLED },
		SPRAYERS               = { name = "Sprayer Tools", state = TypeControlOptions.STATE_ENABLED },
		STONEPICKERS           = { name = "Stone Pickers", state = TypeControlOptions.STATE_ENABLED },
		STRAWBLOWERS           = { name = "Straw Blowers", state = TypeControlOptions.STATE_ENABLED },
		SUBSOILERS             = { name = "Subsoilers", state = TypeControlOptions.STATE_ENABLED },
		SUGARCANEHARVESTERS    = { name = "Sugarcane Harvesters", state = TypeControlOptions.STATE_ENABLED },
		SUGARCANEPLANTERS      = { name = "Sugarcane Planters", state = TypeControlOptions.STATE_ENABLED },
		SUGARCANETRANSPORT     = { name = "Sugarcane Transport", state = TypeControlOptions.STATE_ENABLED },
		TEDDERS                = { name = "Tedders", state = TypeControlOptions.STATE_ENABLED },
		TELELOADERTOOLS        = { name = "Teleloader Tools", state = TypeControlOptions.STATE_DISABLED },
		TELELOADERVEHICLES     = { name = "Teleloader Vehicles", state = TypeControlOptions.STATE_ENABLED },
		TRACTORS               = { name = "Tractors", state = TypeControlOptions.STATE_ENABLED },
		TRAILERS               = { name = "Trailers", state = TypeControlOptions.STATE_ENABLED },
		TRAILERSCHANGINGSYSTEM = { name = "Trailers Changing System", state = TypeControlOptions.STATE_ENABLED },
		TRAILERSFLATBED        = { name = "Trailers Flatbed", state = TypeControlOptions.STATE_ENABLED },
		TRAILERSSEMI           = { name = "Trailers Semi", state = TypeControlOptions.STATE_ENABLED },
		TRUCKS                 = { name = "Trucks", state = TypeControlOptions.STATE_ENABLED },
		VEGETABLEHARVESTERS    = { name = "Vegetable Harvesters", state = TypeControlOptions.STATE_ENABLED },
		VEGETABLEPLANTERS      = { name = "Vegetable Planters", state = TypeControlOptions.STATE_ENABLED },
		WEEDERS                = { name = "Weeders", state = TypeControlOptions.STATE_ENABLED },
		WEIGHTS                = { name = "Weights", state = TypeControlOptions.STATE_DISABLED },
		WHEELLOADERTOOLS       = { name = "Wheel Loader Tools", state = TypeControlOptions.STATE_DISABLED },
		WHEELLOADERVEHICLES    = { name = "Wheel Loader Vehicles", state = TypeControlOptions.STATE_ENABLED },
		WINDROWERS             = { name = "Windrowers", state = TypeControlOptions.STATE_ENABLED },
		WINTEREQUIPMENT        = { name = "Winter Equipment", state = TypeControlOptions.STATE_ENABLED },
		WOODCHIPPERS           = { name = "Wood Chippers", state = TypeControlOptions.STATE_ENABLED },
		WOODTRANSPORT          = { name = "Wood Transport", state = TypeControlOptions.STATE_ENABLED },
	}
end

-- Returns the table sorted by keys - Used to iterate on the TypeControlOptions in a sorted manner
function TypeControlOptions.pairsByKeys(t, f)
	local a = {}
	for n in pairs(t) do table.insert(a, n) end
	table.sort(a, f)
	local i = 0          -- iterator variable
	local iter = function() -- iterator function
		i = i + 1
		if a[i] == nil then
			return nil
		else
			return a[i], t[a[i]]
		end
	end

	return iter
end

function TypeControlOptions.getCategory(storeItem)
	if string.find(storeItem.categoryName, "TRACTORS") then
		-- We group all tractor categories for this
		return "TRACTORS"
	elseif string.find(storeItem.categoryName, "BALERS") then
		-- We group all balers categories for this
		return "BALERS"
	elseif string.find(storeItem.categoryName, "FORAGEHARVESTERCUTTER") then
		-- We group cutters (FORAGEHARVESTERCUTTER) with trailers (FORAGEHARVESTERCUTTERTRAILERS) categories for this
		return "FORAGEHARVESTERCUTTER"
	else
		return storeItem.categoryName
	end
end

function TypeControlOptions.matchesCategory(storeItem)
	return ThroughTheYears.typeControlOptions[TypeControlOptions.getCategory(storeItem)] ~= nil
end
