<?php
/*
** Copyright (C) 2021-2026 initMAX s.r.o.
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/

/**
 * tetrisMAX config form for Zabbix 6.0 / 6.2.
 *
 * ONE PRODUCT: the same field, with the same label and the same "?" tooltip as
 * the 6.4 and 7.0+ forms. What the customer configures is identical; only the
 * container API differs, because the frontends do - 6.2 has CFormGrid
 * (createFormGrid + addItem([label, field])), 6.0 has CFormList
 * (createFormList + addRow(label, field)).
 *
 * @var CView $this
 * @var array $data
 */

use Modules\TetrisMAX\Services\WidgetTranslator;

$fields = $data['dialogue']['fields'];
$form = $data['form'];

$is_grid = method_exists(CWidgetHelper::class, 'createFormGrid');

// The container needs the dialogue's own metadata - the widget name, its type,
// the view mode, the known types and the refresh-rate field. Building it empty
// is an ArgumentCountError on 6.0, so it is built once, here, with everything.
$rf_rate = $data['templateid'] === null ? $fields['rf_rate'] : null;

// Same hint key the 6.4+ form uses, so the explanation reads identically on
// every version. CWidgetHelper::getLabel takes the hint TEXT as its third
// argument and builds the icon itself.
$hint = WidgetTranslator::translate('form.hint.speed');
$label = CWidgetHelper::getLabel($fields['speed'], null,
	$hint !== 'form.hint.speed' ? $hint : null
);
$control = CWidgetHelper::getRadioButtonList($fields['speed']);

$rows = [[$label, $control]];

// PRO field rows from features.yaml teaser_fields - none for tetrisMAX, so
// this adds nothing; kept so the form is wired like every other widget.
$rows = array_merge($rows, \Modules\TetrisMAX\Services\ProTeaser::rows($fields));

if ($is_grid) {
	// Zabbix 6.2
	$container = CWidgetHelper::createFormGrid($data['dialogue']['name'], $data['dialogue']['type'],
		$data['dialogue']['view_mode'], $data['known_widget_types'], $rf_rate
	);
	foreach ($rows as $row) {
		$container->addItem([$row[0], new CFormField($row[1])]);
	}
}
else {
	// Zabbix 6.0
	$container = CWidgetHelper::createFormList($data['dialogue']['name'], $data['dialogue']['type'],
		$data['dialogue']['view_mode'], $data['known_widget_types'], $rf_rate
	);
	foreach ($rows as $row) {
		$container->addRow($row[0], $row[1]);
	}
}

$form->addItem($container);

// Edition badge (initmax-free) + "Give us feedback" link at the top of the
// form, drawn by the CI-injected ProTeaser (window.initmaxProTeaser).
$form->addItem(
	(new CScriptTag(\Modules\TetrisMAX\Services\ProTeaser::initJs($form->getId())))->setOnDocumentReady()
);

return ['form' => $form];
