jingrow.ui.form.on("Local Ai Node", {
	refresh(frm) {
		if (frm.pg.node_type) {
			frm.add_custom_button('编辑节点Schema', function() {
				open_schema_editor(frm);
			});
		}
	}
});

function open_schema_editor(frm) {
	if (!frm.pg.node_type) {
		jingrow.msgprint("请先选择节点类型", "提示");
		return;
	}
	
	let d = new jingrow.ui.Dialog({
		title: `编辑 ${frm.pg.node_type} Schema`,
		size: "extra-large",
		primary_action_label: null,
		primary_action: null,
		secondary_action_label: null,
		secondary_action: null
	});
	
	d.$body.html(`
		<div id="schema-builder-container">
			<div id="schema-builder"></div>
		</div>
	`);
	
	d.show();
	
	init_vue_schema_editor(d, frm);
}

function init_vue_schema_editor(dialog, frm) {
	const container = dialog.$body.find('#schema-builder')[0];
	
	load_schema_builder_components().then(() => {
		create_schema_builder_app(container, frm, dialog);
	}).catch(error => {
		jingrow.msgprint(`加载 Schema 编辑器失败: ${error.message}`, "错误");
	});
}



function create_schema_builder_app(container, frm, dialog) {
	const schemaBuilder = new jingrow.ui.SchemaBuilder({
		wrapper: container,
		frm: frm,
		pagetype: frm.pg.pagetype,
		customize: false
	});
	
	dialog.schema_builder = schemaBuilder;
	
	if (schemaBuilder.store) {
		schemaBuilder.store.saveSchemaToForm = () => {
			schemaBuilder.saveSchemaToForm();
		};
	}
	
	container.addEventListener('schema-save', (event) => {
		const schemaData = event.detail.schemaData;
		if (schemaData) {
			frm.set_value('node_schema', JSON.stringify(schemaData, null, 2));
			frm.save().then(() => {
				jingrow.msgprint("Schema 保存成功");
			}).catch(error => {
				jingrow.msgprint(`保存失败: ${error.message}`, "错误");
			});
		}
	});
	
	if (frm.pg.node_schema) {
		let initialSchema = {};
		if (typeof frm.pg.node_schema === 'string') {
			try {
				initialSchema = JSON.parse(frm.pg.node_schema);
			} catch (e) {
				initialSchema = {};
			}
		} else if (typeof frm.pg.node_schema === 'object') {
			initialSchema = frm.pg.node_schema;
		}
		
		if (schemaBuilder.store && schemaBuilder.store.loadSchema) {
			schemaBuilder.store.loadSchema(initialSchema);
		}
	}
}

function load_schema_builder_components() {
	return new Promise((resolve, reject) => {
		if (jingrow.ui && jingrow.ui.SchemaBuilder) {
			resolve();
			return;
		}
		
		jingrow.require('schema_builder.bundle.js').then(() => {
			if (jingrow.ui && jingrow.ui.SchemaBuilder) {
				resolve();
			} else {
				reject(new Error('组件加载失败'));
			}
		}).catch(error => {
			reject(error);
		});
	});
}

function save_schema_from_editor(frm, dialog) {
	try {
		let schemaData = null;
		if (dialog.schema_builder && typeof dialog.schema_builder.getSchemaData === 'function') {
			try {
				schemaData = dialog.schema_builder.getSchemaData();
			} catch (e) {}
		}
		
		if (!schemaData) {
			jingrow.msgprint("无法获取 Schema 数据", "错误");
			return;
		}
		
		const prettyJson = JSON.stringify(schemaData, null, 2);
		frm.set_value('node_schema', prettyJson);
		
		frm.save().then(() => {
			if (dialog.schema_builder && dialog.schema_builder.store) {
				dialog.schema_builder.store.dirty = false;
			}
					jingrow.msgprint("Schema 保存成功");
		dialog.hide();
		}).catch((error) => {
			jingrow.msgprint("保存失败", "错误");
		});
		
	} catch (e) {
		jingrow.msgprint(`保存失败: ${e.message}`, "错误");
	}
}

