context("Dynamic Link", () => {
	before(() => {
		cy.login();
		cy.visit("/app/pagetype");
		return cy
			.window()
			.its("jingrow")
			.then((jingrow) => {
				return jingrow.xcall("jingrow.tests.ui_test_helpers.create_pagetype", {
					name: "Test Dynamic Link",
					fields: [
						{
							label: "Document Type",
							fieldname: "pg_type",
							fieldtype: "Link",
							options: "PageType",
							in_list_view: 1,
							in_standard_filter: 1,
						},
						{
							label: "Document ID",
							fieldname: "pg_id",
							fieldtype: "Dynamic Link",
							options: "pg_type",
							in_list_view: 1,
							in_standard_filter: 1,
						},
					],
				});
			});
	});

	function get_dialog_with_dynamic_link() {
		return cy.dialog({
			title: "Dynamic Link",
			fields: [
				{
					label: "Document Type",
					fieldname: "pg_type",
					fieldtype: "Link",
					options: "PageType",
					in_list_view: 1,
				},
				{
					label: "Document ID",
					fieldname: "pg_id",
					fieldtype: "Dynamic Link",
					options: "pg_type",
					in_list_view: 1,
				},
			],
		});
	}

	function get_dialog_with_dynamic_link_option() {
		return cy.dialog({
			title: "Dynamic Link",
			fields: [
				{
					label: "Document Type",
					fieldname: "pg_type",
					fieldtype: "Link",
					options: "PageType",
					in_list_view: 1,
				},
				{
					label: "Document ID",
					fieldname: "pg_id",
					fieldtype: "Dynamic Link",
					get_options: () => {
						return "User";
					},
					in_list_view: 1,
				},
			],
		});
	}

	it("Creating a dynamic link by passing option as function and verifying it in a dialog", () => {
		get_dialog_with_dynamic_link_option().as("dialog");
		cy.get_field("pg_type").clear();
		cy.fill_field("pg_type", "User", "Link");
		cy.get_field("pg_id").click();

		//Checking if the listbox have length greater than 0
		cy.get('[data-fieldname="pg_id"]')
			.find(".awesomplete")
			.find("div")
			.its("length")
			.should("be.gte", 0);
		cy.get(".btn-modal-close").click({ force: true });
	});

	it("Creating a dynamic link and verifying it in a dialog", () => {
		get_dialog_with_dynamic_link().as("dialog");
		cy.get_field("pg_type").clear();
		cy.fill_field("pg_type", "User", "Link");
		cy.get_field("pg_id").click();

		//Checking if the listbox have length greater than 0
		cy.get('[data-fieldname="pg_id"]')
			.find(".awesomplete")
			.find("div")
			.its("length")
			.should("be.gte", 0);
		cy.get(".btn-modal-close").click({ force: true, multiple: true });
	});

	it("Creating a dynamic link and verifying it", () => {
		cy.visit("/app/test-dynamic-link");

		//Clicking on the Document ID field
		cy.get_field("pg_type").clear();

		//Entering User in the Pagetype field
		cy.fill_field("pg_type", "User", "Link", { delay: 500 });
		cy.get_field("pg_id").click();

		//Checking if the listbox have length greater than 0
		cy.get('[data-fieldname="pg_id"]')
			.find(".awesomplete")
			.find("div")
			.its("length")
			.should("be.gte", 0);

		//Opening a new form for dynamic link pagetype
		cy.new_form("Test Dynamic Link");
		cy.get_field("pg_type").clear();

		//Entering User in the Pagetype field
		cy.fill_field("pg_type", "User", "Link", { delay: 500 });
		cy.get_field("pg_id").click();

		//Checking if the listbox have length greater than 0
		cy.get('[data-fieldname="pg_id"]')
			.find(".awesomplete")
			.find("div")
			.its("length")
			.should("be.gte", 0);
		cy.get_field("pg_type").clear();

		//Entering System Settings in the Pagetype field
		cy.intercept("/api/action/jingrow.desk.search.search_link").as("search_query");
		cy.fill_field("pg_type", "System Settings", "Link", { delay: 500 });
		cy.wait("@search_query");
		cy.get(`[data-fieldname="pg_type"] ul:visible div:first-child`).click({
			scrollBehavior: false,
		});

		cy.get_field("pg_id").click();

		//Checking if the system throws error
		cy.get(".modal-title").should("have.text", "Error");
		cy.get(".msgprint").should(
			"have.text",
			"System Settings is not a valid PageType for Dynamic Link"
		);
	});
});
