// form/nc/nc-work-center/nc-work-center-util.js

export function updateCurrentAndPersonFunction(state, nc, dt, message) {
	const rec = state.rec
	const hdr = state.hdr
	const arr = state.arr
	const recData = nc.recData
	const recDataSet = nc.recDataSet

	function setDefaultToCurrent(rowRec) {
		let value
		for (const key of rec.current_field_set) {
			value = rowRec && recData(rowRec, key)
			if (value == null) {
				value = rec.current_field_set_default[key]
			}
			if (value != null) {
				if (typeof rec.current_field_set_default[key] === 'number') {
					recDataSet(rec.current, key, Number(value))
				} else {
					recDataSet(rec.current, key, value)
				}
			}
		}
	}

	function setField(infoField, infoRec) {
		let prevConcatValue = ''
		rec.current.calc[infoField] = ''
		let val
		for (const key of rec[infoField + '_field']) {
			val = infoRec && recData(infoRec, key)
			if (val != null) {
				if (val !== '') {
					if (rec.field_format[key]) {
						val = dt.formatDate(val, rec.field_format[key])
					}
					rec.current.calc[infoField] += prevConcatValue + val
				}
			} else {
				prevConcatValue = key
			}
		}
	}

	function setPersonStateToCurrent(workPhaseRec) {
		let value
		for (const key of Object.keys(rec.person_state_field)) {
			value = workPhaseRec[key]
			if (value == null) {
				value = rec.person_state_field[key]
			}
			recDataSet(rec.current, key, value)
		}
	}

	function setPrev() {
		rec.current.prev_started_amount = rec.current.started_amount
		rec.current.prev_produced_amount = rec.current.produced_amount
		rec.current.prev_failed_amount = rec.current.failed_amount
		rec.current.prev_info = rec.current.info
	}

	function fixPerson() {
		if ((arr.person.length != 1 || arr.person[0].value != '') && nc.recordArrayIndex(arr.person, 'value', rec.person) < 0) {
			rec.person = ''
		}
	}

	return function updateCurrentAndPerson(fromRec, workPhaseRec) {
		fixPerson()
		const personWorkPhase = state.data?.work_phase_actual_open?.filter(item => item.person_id === rec.person) || []
		const personOpenWorkPhase = personWorkPhase.filter(item => dt.isZeroDate(item.end_time))
		const unproductiveWorkPhase = personOpenWorkPhase.filter(
			item => item.production_order_id === rec.constant.unproductive_id && item.work_phase_state !== rec.constant.unproductive_work_change_state
		)
		if (unproductiveWorkPhase.length > 1) {
			message.setWarning(state, 'more than one unproductive work phase was found')
		}
		if (unproductiveWorkPhase.length >= 1) {
			rec.current.unproductive = unproductiveWorkPhase[0].work_phase_state
		} else {
			rec.current.unproductive = ''
		}
		rec.current.enterable = !!rec.person // to boolean
		if (rec.selected_work_row <= 0) {
			setDefaultToCurrent()
			setField('time_info')
			setField('amount_info')
			setField('work_info')
			setField('done_info', rec.current.calc)
			rec.current.started_amount = 0
			rec.current.info = ''
			setPrev()
			rec.current.enterable = false
			rec.current.can_run = false
			rec.current.can_start = false
			return
		}
		setPrev()
		let rowRec = fromRec || state.grid.work.data[rec.selected_work_row - 1] // nc.grid.activatedRow(state, 'work')
		const machineWorkPhase =
			rowRec &&
			personOpenWorkPhase.find(item => item.production_order_id === rowRec.production_order_id && item.work_phase_number === rowRec.work_phase_number && item.serie === 'machine')
		if (machineWorkPhase) {
			rec.machine_state = machineWorkPhase.work_phase_state
			rec.run_machine = machineWorkPhase.work_phase_state
		} else {
			rec.machine_state = ''
			rec.run_machine = 'running'
		}
		rec.run_machine_idx = nc.recordArrayIndex(arr.run_machine, 'value', rec.run_machine) || 0
		if (workPhaseRec == null && rowRec) {
			workPhaseRec = personOpenWorkPhase.find(
				item => item.production_order_id === rowRec.production_order_id && item.work_phase_number === rowRec.work_phase_number && item.serie === 'person'
			)
			if (!workPhaseRec && unproductiveWorkPhase.length >= 1) {
				rec.current.can_start = false
				rec.current.can_setup = false
				rec.current.can_run = false
				workPhaseRec = personWorkPhase.find(
					item => item.production_order_id === rowRec.production_order_id && item.work_phase_number === rowRec.work_phase_number && item.serie === 'person'
				)
			}
		}
		if (!workPhaseRec) {
			workPhaseRec = {}
			rec.current.started_amount = 0
			rec.current.produced_amount = 0
			rec.current.failed_amount = 0
			rec.current.info = ''
			setPrev()
		}
		if (!workPhaseRec?.work_phase_state) {
			workPhaseRec.work_phase_state = ''
		}
		rec.person_state = workPhaseRec?.work_phase_state || ''
		if (rec.person_state !== '') {
			rec.info_open = true
		}
		function setState(state) {
			if (state == null) {
				message.setError(state, `empty set state`)
			} else if (state === rec.constant.phase_state.close) {
				hdr.work_center.action = hdr.work_center.close
				// rec.action_icon = rec.button_icon.close
				// rec.primary_action = rec.constant.phase_state.close
			} else if (state === rec.constant.phase_state.edit) {
				hdr.work_center.action = hdr.work_center.edit
				// rec.action_icon = rec.button_icon.edit
				// rec.primary_action = rec.constant.phase_state.edit
			} else if (state === rec.constant.phase_state.end) {
				hdr.work_center.action = hdr.work_center.end
				// rec.action_icon = rec.button_icon.end
				// rec.primary_action = rec.constant.phase_state.end
			} else if (state === rec.constant.phase_state.stop) {
				hdr.work_center.action = hdr.work_center.stop
				// rec.action_icon = rec.button_icon.stop
				// rec.primary_action = rec.constant.phase_state.stop
			} else if (state === rec.constant.phase_state.empty) {
				hdr.work_center.action = hdr.work_center.edit
				rec.person_state = rec.constant.phase_state.empty
				// rec.action_icon = rec.button_icon.play
			} else {
				message.setError(state, `unknown set state ${state}`)
			}
		}
		// debugger
		if (!workPhaseRec.work_phase_state) {
			setState(rec.constant.phase_state.edit)
		} else if (workPhaseRec.work_phase_state === rec.constant.phase_state.stopping) {
			setState(rec.constant.phase_state.end)
		} else if (workPhaseRec.work_phase_state === rec.constant.phase_state.edit) {
			setState(rec.constant.phase_state.close)
		} else if (workPhaseRec.work_phase_state === rec.constant.phase_state.material) {
			setState(rec.constant.phase_state.stop)
		} else if (workPhaseRec.work_phase_state === rec.constant.phase_state.setup) {
			setState(rec.constant.phase_state.stop)
		} else if (workPhaseRec.work_phase_state === rec.constant.phase_state.working) {
			setState(rec.constant.phase_state.stop)
		} else if (workPhaseRec.work_phase_state === rec.constant.phase_state.stop) {
			setState(rec.constant.phase_state.stop)
		} else {
			message.setError(state, `unknown person state ${rowRec.work_phase_state}, ${rec.person_state}`)
			setState(rec.constant.phase_state.empty)
		}
		if (workPhaseRec.production_order_id === rec.constant.unproductive_id) {
			rec.person_state = rec.constant.unproductive_id
			rec.person_state_icon =
				arr.person_state_unproductive.find(item => item.value === rowRec.work_phase_state)?.icon ||
				arr.person_state_unproductive_button.find(item => item.value === rowRec.work_phase_state)?.icon
		}
		setDefaultToCurrent(rowRec)
		setField('time_info', rowRec)
		setField('amount_info', rowRec)
		setField('work_info', rowRec)
		setField('done_info', rec.current.calc)
		setPersonStateToCurrent(workPhaseRec)
		rec.current.virtual = rec.current.production_order_id.startsWith(rec.constant.virtual_production_order_prefix)
		rec.current.can_start = rec.constant.production_order_state_can_start[rec.current.pro.production_order_state]
		rec.current.can_run = rec.current.started_amount >= rec.min_start_amount
		if (!rec.person || rec.selected_work_row < 1) {
			rec.current.can_start = false
			rec.current.can_run = false
		}
		rec.current.can_setup = rec.current.can_run
		if (rec.person_state === '' && rec.current.started_amount <= 0) {
			rec.current.can_setup = false
			rec.current.can_run = false
		}
		if (rec.current.started_amount < rec.current.produced_amount + rec.current.failed_amount || unproductiveWorkPhase.length >= 1) {
			rec.current.can_start = false
			rec.current.can_setup = false
			rec.current.can_run = false
		}
		if (rec.current.started_amount === 0 || rec.current.started_amount === '') {
			rec.current.started_amount = 0
			rec.current.can_start = false
			rec.current.can_setup = false
			rec.current.can_run = false
		}
	}
}
