export interface Constraints {
	maxDimensions: boolean;
	sheetAvailability: boolean;
	minContourSize: boolean;
	dataConsistent: boolean;
	dataCompleteness: boolean;
	dataValidity: boolean;
	bendDie: boolean;
	// Sheet thickness of bend deduction table row does not match actual sheet thickness
	bendThickness: boolean;
	transportSource: boolean;
	userDefinedProcessId: boolean;
	maxBendLineNetLength: boolean;
	// Underlying sheet thickness too large
	maxSheetThickness: boolean;
	// Bend areas overlap
	bendAreasNotOverlapping: boolean;
	profileSupport: boolean;
	tubeAvailability: boolean;
	tubeNestingAvailability: boolean;
	// The tube cutting process must be able to process the associated tube (if any)
	tubeCuttingProcessCompatibility: boolean;
	// Enforce tube detection feature
	tubeDetectionLicensed: boolean;
	contourInBend: boolean;
	bendFlangeTooShort: boolean;
	sheetTappingDataValid: boolean;
	incompatibleMaterialFound: boolean;
}

export function checkConstraints(vertex: Vertex): Constraints {
	const constraints: Constraints = {
		maxDimensions: false,
		sheetAvailability: false,
		minContourSize: false,
		dataConsistent: false,
		dataCompleteness: false,
		dataValidity: false,
		bendDie: false,
		bendThickness: false,
		transportSource: false,
		userDefinedProcessId: false,
		maxBendLineNetLength: false,
		maxSheetThickness: false,
		bendAreasNotOverlapping: false,
		profileSupport: false,
		tubeAvailability: false,
		tubeNestingAvailability: false,
		tubeCuttingProcessCompatibility: false,
		tubeDetectionLicensed: false,
		contourInBend: false,
		bendFlangeTooShort: false,
		sheetTappingDataValid: false,
		incompatibleMaterialFound: false,
	};

	const wst = wsi4.node.workStepType(vertex);
	const violatedConstraints = wsi4.node.violatedConstraints(vertex);
	violatedConstraints.forEach(c => {
		switch (c.type) {
			case "bendDeductionThicknessInBounds": constraints.bendThickness = true; break;
			case "bendDiesAssigned": constraints.bendDie = true; break;
			case "bendFlangeLengthInBounds": constraints.bendFlangeTooShort = true; break;
			case "bendLineLengthInBounds": constraints.maxBendLineNetLength = true; break;
			case "contourBendZoneUnaffected": constraints.contourInBend = true; break;
			case "dimensionsInBounds": constraints.maxDimensions = true; break;
			case "editingStateOk": {
				constraints.dataCompleteness = c.content.dataMissing;
				constraints.dataValidity = c.content.dataInvalid;
				break;
			}
			case "materialsCompatible": constraints.incompatibleMaterialFound = true; break;
			case "semimanufacturedAvailable": {
				constraints.sheetAvailability = wst === "sheet";
				constraints.tubeAvailability = wst === "tube";
				break;
			}
			case "sheetCuttingContourSizeInBounds": constraints.minContourSize = true; break;
			case "sheetCuttingProcessCompatible": break;
			case "sheetCuttingThicknessInBounds": constraints.maxSheetThickness = true; break;
			case "tubeCuttingProcessCompatible": constraints.tubeCuttingProcessCompatibility = true; break;
			case "tubeCuttingProfileSupported": constraints.profileSupport = true; break;
			case "tubeDetectionLicensed": constraints.tubeDetectionLicensed = true; break;
			case "unfoldingSimple": constraints.bendAreasNotOverlapping = true; break;
		}
	});

	return constraints;
}
