// plugin/nc-calc-server/convert/product-convert.js

const convert = {}
export default convert
let nc = null

export function setState(ncObject) {
	nc = ncObject
}
convert.setState = setState

function round(value, decimals) {
	return nc.round(value, decimals)
}

export function amountStock1ToStock2(pr, amount, decimals) {
	let newAmount = 0
	if (pr == null || pr.unit_multiplier == null) {
		newAmount = 0
	} else if (pr.unit_multiplier === 0) {
		newAmount = 0
	} else if (pr.unit_multiplier === 1) {
		newAmount = amount
	} else if (pr.unit_multiplier !== -1) {
		newAmount = amount / pr.unit_multiplier
	} else if (amount !== 0) {
		newAmount = 1 / amount
	}
	return round(newAmount, decimals)
}
convert.amountStock1ToStock2 = amountStock1ToStock2

export function amountStock2ToStock1(pr, amount, decimals) {
	let newAmount = 0
	if (pr == null || pr.unit_multiplier == null) {
		newAmount = 0
	} else if (pr.unit_multiplier === 0) {
		newAmount = 0
	} else if (pr.unit_multiplier === 1) {
		newAmount = amount
	} else if (pr.unit_multiplier !== -1) {
		newAmount = amount * pr.unit_multiplier
	} else if (amount !== 0) {
		newAmount = 1 / amount
	}
	return round(newAmount, decimals)
}
convert.amountStock2ToStock1 = amountStock2ToStock1

export function priceStock1ToStock2(product, price) {
	const prResult = {} // qry.queryJson('plugin/nc-calc-server/convert/product_arr_conversion_multiplier.json', { product_arr: [product] })
	if (prResult && prResult.error) {
		message.setWarning(state, prResult.error)
		return 0
	}
	const pr = prResult.data && prResult.data[0]
	if (pr == null) {
		message.setWarning(state, nc.l("product '%s' conversion table is missing", String(product)))
		return 0
	}
	let newPrice = 0
	if (pr.unit_multiplier == null) {
		newPrice = 0
	} else if (pr.unit_multiplier === 0) {
		newPrice = 0
	} else if (pr.unit_multiplier === 1) {
		newPrice = price
	} else if (pr.unit_multiplier !== -1) {
		newPrice = price * pr.unit_multiplier
	} else if (price !== 0) {
		newPrice = 1 / newPrice
	}
	return newPrice
}
convert.priceStock1ToStock2 = priceStock1ToStock2

export function priceStock2ToStock1(product, price) {
	const prResult = {} //  qry.queryJson('plugin/nc-calc-server/convert/product_arr_conversion_multiplier.json', { product_arr: [product] })
	if (prResult && prResult.error) {
		message.setWarning(state, prResult.error)
		return 0
	}
	const pr = prResult.data && prResult.data[0]
	if (pr == null) {
		message.setWarning(state, nc.l("product '%s' conversion table is missing", String(product)))
		return 0
	}
	let newPrice = 0
	if (pr.unit_multiplier == null) {
		newPrice = 0
	} else if (pr.unit_multiplier === 0) {
		newPrice = 0
	} else if (pr.unit_multiplier === 1) {
		newPrice = price
	} else if (pr.unit_multiplier !== -1) {
		newPrice = price / pr.unit_multiplier
	} else if (price !== 0) {
		newPrice = 1 / price
	}
	return newPrice
}
convert.priceStock2ToStock1 = priceStock2ToStock1

function amountInStockUnit14d(pr, amount, unit, productId, workNumberId, printError) {
	if (pr == null) {
		message.setWarning(state, nc.l("product '%s' amount conversion product is missing", String(product)))
		return amount
	}
	if (productId == null) {
		if (pr.product_id) {
			productId = pr.product_id
		} else if (pr['product_id - work']) {
			productId = pr['product_id - work']
		}
	}
	if (amount === 0) {
		return 0
	} else if (unit == null) {
		message.setWarning(state, nc.l("product '%s' amount conversion unit parameter is missing", String(product)))
		return amount
	}
	let value
	if (pr.stock_unit === unit) {
		value = amount
	} else if (pr.json_data && pr.json_data.stock_unit_2 === unit) {
		if (pr.unit_multiplier !== -1) {
			value = amount * pr.unit_multiplier
		} else {
			value = 1 / amount
		}
	} else if (pr.design_unit === unit) {
		if (pr.json_data && pr.json_data.design_unit_2 === pr.stock_unit) {
			if (pr.json_data.design_conversion_multiplier !== -1) {
				value = amount / pr.json_data.design_conversion_multiplier
			} else {
				value = 1 / amount
			}
		} else {
			if (pr.unit_multiplier !== -1) {
				value = amount * pr.unit_multiplier
			} else {
				value = 1 / amount
			}
		}
	} else if (pr.json_data && pr.json_data.design_unit_2 === unit) {
		let valueDesign1
		if (pr.json_data.design_conversion_multiplier !== -1) {
			valueDesign1 = amount * pr.json_data.design_conversion_multiplier
		} else {
			valueDesign1 = 1 / amount
		}
		if (pr.unit_multiplier !== -1) {
			value = valueDesign1 * pr.unit_multiplier
		} else if (valueDesign1 !== 0) {
			value = 1 / valueDesign1
		}
	} else {
		let err = nc.l(
			"product '%s' amount conversion failed, can't convert unit '%s' amount %.4f to product stock unit '%s', work '%s'",
			String(productId),
			String(unit),
			Number(amount),
			String(pr.stock_unit),
			String(workNumberId || '')
		)
		if (printError !== false) {
			err = message.setWarning(state, err)
		}
		return amount
	}
	return value
}

export function amountInStockUnit1(pr, amount, unit, productId, workNumberId, schema, printError) {
	if (schema === '4d') {
		return amountInStockUnit14d(pr, amount, unit, productId, workNumberId, printError)
	}
	return amount
}
convert.amountInStockUnit1 = amountInStockUnit1

export function amountDesign1ToStock1(pr, amount, decimals) {
	let newAmount = 0
	if (pr == null || pr.json_data?.unit_design_unit_multiplier == null) {
		newAmount = 0
	} else if (pr.json_data.unit_design_unit_multiplier === 0) {
		newAmount = 0
	} else if (pr.json_data.unit_design_unit_multiplier === 1) {
		newAmount = amount
	} else if (pr.json_data.unit_design_unit_multiplier !== -1) {
		newAmount = amount * pr.json_data.unit_design_unit_multiplier
	} else if (amount !== 0) {
		newAmount = 1 / amount
	}
	return round(newAmount, decimals)
}
convert.amountDesign1ToStock1 = amountDesign1ToStock1

// Convert from stock unit 1 to design unit 1
export function amountStock1ToDesign1(pr, amount, decimals) {
	let newAmount = 0
	if (pr == null || pr.json_data?.unit_design_unit_multiplier == null) {
		newAmount = 0
	} else if (pr.json_data.unit_design_unit_multiplier === 0) {
		newAmount = 0
	} else if (pr.json_data.unit_design_unit_multiplier === 1) {
		newAmount = amount
	} else if (pr.json_data.unit_design_unit_multiplier !== -1) {
		newAmount = amount / pr.json_data.unit_design_unit_multiplier
	} else if (amount !== 0) {
		newAmount = 1 / amount
	}
	return round(newAmount, decimals)
}
convert.amountStock1ToDesign1 = amountStock1ToDesign1

// Convert from stock unit 1 to design unit 2
export function amountStock1ToDesign2(pr, amount, decimals) {
	if (pr == null || pr.json_data?.design_conversion_multiplier == null) {
		return 0
	}
	const amountInDesign1 = amountStock1ToDesign1(pr, amount)
	let newAmount = 0
	if (pr.json_data.design_conversion_multiplier === 0) {
		newAmount = 0
	} else if (pr.json_data.design_conversion_multiplier === 1) {
		newAmount = amountInDesign1
	} else if (pr.json_data.design_conversion_multiplier !== -1) {
		newAmount = amountInDesign1 / pr.json_data.design_conversion_multiplier
	} else if (amountInDesign1 !== 0) {
		newAmount = 1 / amountInDesign1
	} else {
		newAmount = 0
	}
	return round(newAmount, decimals)
}
convert.amountStock1ToDesign2 = amountStock1ToDesign2

// Convert from design unit 2 to stock unit 1
export function amountDesign2ToStock1(pr, amount, decimals) {
	let newAmount = 0
	if (pr == null || pr.json_data?.design_conversion_multiplier == null) {
		newAmount = 0
	} else if (pr.json_data.design_conversion_multiplier === 0) {
		newAmount = 0
	} else if (pr.json_data.design_conversion_multiplier === 1) {
		newAmount = amount
	} else if (pr.json_data.design_conversion_multiplier !== -1) {
		newAmount = amount * pr.json_data.design_conversion_multiplier
	} else if (amount !== 0) {
		newAmount = 1 / amount
	}
	return round(newAmount, decimals)
}
convert.amountDesign2ToStock1 = amountDesign2ToStock1

export function calculatePacketSize(packet, changedField) {
	if (packet.packet_count <= 0 || packet.stock_amount_1 === 0) {
		packet.stock_amount_full_packet_1 = 0
		packet.stock_amount_full_packet_2 = 0
		packet.stock_amount_last_packet_1 = 0
		return
	}
	if (packet.stock_amount_1 < 1) {
		packet.stock_amount_full_packet_1 = packet.stock_amount_1
		packet.stock_amount_last_packet_1 = 0
		return
	}
	if (changedField === 'stock_amount_full_packet_1') {
		if (packet.packet_count === 0) {
			packet.packet_count = Math.trunc(packet.stock_amount_1 / packet.stock_amount_full_packet_1)
			packet.stock_amount_last_packet_1 = packet.stock_amount_1 - packet.stock_amount_full_packet_1 * packet.packet_count
		} else if (packet.stock_amount_1 > packet.packet_count * packet.stock_amount_full_packet_1) {
			packet.stock_amount_last_packet_1 = packet.stock_amount_1 - packet.stock_amount_full_packet_1 * packet.packet_count
		} else if (packet.stock_amount_1 < packet.packet_count * packet.stock_amount_full_packet_1) {
			packet.stock_amount_last_packet_1 = packet.stock_amount_1 - packet.stock_amount_full_packet_1 * (packet.packet_count - 1) - packet.stock_amount_full_packet_1
		} else {
			packet.stock_amount_last_packet_1 = 0
		}
	} else if (changedField === 'stock_amount_full_packet_2' || changedField === 'packet_count_2' || changedField === 'stock_amount_last_packet_2') {
		packet.stock_amount_full_packet_1 = Math.trunc(packet.stock_amount_1 / packet.packet_count)
		packet.stock_amount_last_packet_1 = packet.stock_amount_1 - packet.packet_count * packet.stock_amount_full_packet_1
	} else {
		packet.stock_amount_full_packet_1 = Math.trunc(packet.stock_amount_1 / packet.packet_count)
		packet.stock_amount_last_packet_1 = packet.stock_amount_1 - packet.stock_amount_full_packet_1 * packet.packet_count
	}
}
convert.calculatePacketSize = calculatePacketSize

export function unitAmount(changedField, packet, decimals) {
	const pr = packet?.row?.pr
	if (pr) {
		if (changedField === 'stock_amount_2' || changedField === 'stock_amount_1') {
			if (changedField === 'stock_amount_2') {
				packet.stock_amount_1 = amountStock2ToStock1(pr, packet.stock_amount_2, decimals)
			}
		} else if (changedField === 'stock_amount_full_packet_2' || changedField === 'stock_amount_full_packet_1') {
			if (changedField === 'stock_amount_full_packet_2') {
				packet.stock_amount_full_packet_1 = amountStock2ToStock1(pr, packet.stock_amount_full_packet_2, decimals)
			}
		} else if (changedField === 'stock_amount_last_packet_2' || changedField === 'stock_amount_last_packet_1') {
			if (changedField === 'stock_amount_last_packet_2') {
				packet.stock_amount_last_packet_1 = amountStock2ToStock1(pr, packet.stock_amount_last_packet_2, decimals)
			}
		} else if (changedField === 'design_amount_1') {
			packet.stock_amount_1 = amountDesign1ToStock1(pr, packet.design_amount_1, decimals)
		} else if (changedField === 'design_amount_2') {
			let amountInDesign1
			if (pr.json_data.design_conversion_multiplier == null) {
				amountInDesign1 = 0
			} else if (pr.json_data.design_conversion_multiplier === 0) {
				amountInDesign1 = 0
			} else if (pr.json_data.design_conversion_multiplier === 1) {
				amountInDesign1 = packet.design_amount_2
			} else if (pr.json_data.design_conversion_multiplier !== -1) {
				amountInDesign1 = packet.design_amount_2 * pr.json_data.design_conversion_multiplier
			} else if (packet.design_amount_2 !== 0) {
				amountInDesign1 = 1 / packet.design_amount_2
			} else {
				amountInDesign1 = 0
			}
			packet.stock_amount_1 = amountDesign1ToStock1(pr, amountInDesign1, decimals)
		} else if (changedField === 'design_amount_full_packet_1') {
			packet.stock_amount_full_packet_1 = amountDesign1ToStock1(pr, packet.design_amount_full_packet_1, decimals)
		} else if (changedField === 'design_amount_full_packet_2') {
			let amountInDesign1
			if (pr.json_data.design_conversion_multiplier == null) {
				amountInDesign1 = 0
			} else if (pr.json_data.design_conversion_multiplier === 0) {
				amountInDesign1 = 0
			} else if (pr.json_data.design_conversion_multiplier === 1) {
				amountInDesign1 = packet.design_amount_full_packet_2
			} else if (pr.json_data.design_conversion_multiplier !== -1) {
				amountInDesign1 = packet.design_amount_full_packet_2 * pr.json_data.design_conversion_multiplier
			} else if (packet.design_amount_full_packet_2 !== 0) {
				amountInDesign1 = 1 / packet.design_amount_full_packet_2
			} else {
				amountInDesign1 = 0
			}
			packet.stock_amount_full_packet_1 = amountDesign1ToStock1(pr, amountInDesign1, decimals)
		} else if (changedField === 'design_amount_last_packet_1') {
			packet.stock_amount_last_packet_1 = amountDesign1ToStock1(pr, packet.design_amount_last_packet_1, decimals)
		} else if (changedField === 'design_amount_last_packet_2') {
			let amountInDesign1
			if (pr.json_data.design_conversion_multiplier == null) {
				amountInDesign1 = 0
			} else if (pr.json_data.design_conversion_multiplier === 0) {
				amountInDesign1 = 0
			} else if (pr.json_data.design_conversion_multiplier === 1) {
				amountInDesign1 = packet.design_amount_last_packet_2
			} else if (pr.json_data.design_conversion_multiplier !== -1) {
				amountInDesign1 = packet.design_amount_last_packet_2 * pr.json_data.design_conversion_multiplier
			} else if (packet.design_amount_last_packet_2 !== 0) {
				amountInDesign1 = 1 / packet.design_amount_last_packet_2
			} else {
				amountInDesign1 = 0
			}
			packet.stock_amount_last_packet_1 = amountDesign1ToStock1(pr, amountInDesign1, decimals)
		} else {
			if (changedField !== '') {
				console.error(
					`unitAmount: changedField '${changedField}' is not handled. Valid fields are: stock_amount_1, stock_amount_2, stock_amount_full_packet_1, stock_amount_full_packet_2, stock_amount_last_packet_1, stock_amount_last_packet_2, design_amount_1, design_amount_2, design_amount_full_packet_1, design_amount_full_packet_2, design_amount_last_packet_1, design_amount_last_packet_2`
				)
				return
			}
		}
		packet.stock_amount_2 = amountStock1ToStock2(pr, packet.stock_amount_1, decimals)
		packet.stock_amount_full_packet_2 = amountStock1ToStock2(pr, packet.stock_amount_full_packet_1, decimals)
		packet.stock_amount_last_packet_2 = amountStock1ToStock2(pr, packet.stock_amount_last_packet_1, decimals)
		packet.design_amount_1 = amountStock1ToDesign1(pr, packet.stock_amount_1, decimals)
		packet.design_amount_2 = amountStock1ToDesign2(pr, packet.stock_amount_1, decimals)
		packet.design_amount_full_packet_1 = amountStock1ToDesign1(pr, packet.stock_amount_full_packet_1, decimals)
		packet.design_amount_full_packet_2 = amountStock1ToDesign2(pr, packet.stock_amount_full_packet_1, decimals)
		packet.design_amount_last_packet_1 = amountStock1ToDesign1(pr, packet.stock_amount_last_packet_1, decimals)
		packet.design_amount_last_packet_2 = amountStock1ToDesign2(pr, packet.stock_amount_last_packet_1, decimals)
	}
}
convert.unitAmount = unitAmount

export function updatePacket(changedField, packet, excludeFromNumberFields) {
	if (changedField !== '') {
		if (
			excludeFromNumberFields !== true &&
			changedField !== 'stock_amount_2' &&
			changedField !== 'design_amount_1' &&
			changedField !== 'design_amount_2' &&
			changedField !== 'stock_amount_last_packet_1' &&
			changedField !== 'design_amount_full_packet_1' &&
			changedField !== 'design_amount_full_packet_2' &&
			changedField !== 'design_amount_last_packet_1' &&
			changedField !== 'design_amount_last_packet_2'
		) {
			packet[changedField] = Number(packet[changedField])
		}
		unitAmount(changedField, packet)
		calculatePacketSize(packet, changedField)
	}
}
convert.updatePacket = updatePacket

export function getUnitCount(pr) {
	if (pr == null) {
		return {
			unitCount: 0,
			showUnit2: false,
			showDesignUnit: false,
			showDesignUnit2: false,
			showDesignMultiplier: false,
			showStockDesignMultiplier: false
		}
	}
	const stockUnit1 = pr.stock_unit
	const stockUnit2 = pr.json_data?.stock_unit_2
	const designUnit1 = pr.design_unit
	const designUnit2 = pr.json_data?.design_unit_2
	const designMultiplier = pr.json_data?.design_conversion_multiplier
	const stockDesignMultiplier = pr.json_data?.unit_design_unit_multiplier
	let showUnit2 = false
	if (stockUnit2 != null && stockUnit2 !== '' && stockUnit2 !== stockUnit1) {
		showUnit2 = true
	}
	let showDesignUnit = false
	if (designUnit1 != null && designUnit1 !== '' && designUnit1 !== stockUnit1) {
		showDesignUnit = true
	}
	let showDesignUnit2 = false
	let showDesignMultiplier = false
	if (designMultiplier !== -1 && designUnit2 != null && designUnit2 !== '') {
		showDesignUnit2 = true
		showDesignMultiplier = true
	}
	let showStockDesignMultiplier = false
	if (stockDesignMultiplier != null && stockDesignMultiplier > 0) {
		showStockDesignMultiplier = true
	}
	const unitCount = 1 + (showUnit2 ? 1 : 0) + (showDesignUnit ? 1 : 0) + (showDesignUnit2 ? 1 : 0)
	return { unitCount, showUnit2, showDesignUnit, showDesignUnit2, showDesignMultiplier, showStockDesignMultiplier }
}
convert.getUnitCount = getUnitCount
