export function selectClick(testid: string, eq = 0) {
  cy.getTestId(testid).click();
  cy.getTestId(testid).find('.rc-virtual-list-holder-inner')
    .children().eq(eq).click();
}

export function menuTrigger(parent: number, child: number) {
  cy.getTestId('menu').children().each(function(el, idx) {
    if (idx === parent) {
      el.find('.ant-menu-submenu-title').trigger('click');
      el.find('ul>li').children().eq(child).trigger('click');
    }
  });
}

export function validateSelect(testid: string, value: string) {
  cy.getTestId(testid).find('.ant-select-selection-item')
    .should('have.attr', 'title', value).and('have.text', value);
}

export function validateTableList(tableName: string, firstPageNumber = 3, secondPageNumber = 2) {
  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').should('have.length', firstPageNumber);

  cy.getTestId(tableName).siblings('.ant-pagination').find('li[title="2"]').click();

  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').should('have.length', secondPageNumber);
}

export function validateTableSearch(
  tableName: string,
  value?: string,
  options?: {btnTestId?: string},
) {
  cy.getTestId(tableName).siblings('.ant-pagination').find('li[title="2"]')
    .click();
  const {btnTestId} = options ?? {};

  return function(text?: string) {
    cy.getTestId(btnTestId ?? 'search_btn').click();
    cy.getTestId(btnTestId ?? 'search_btn').should('have.class', 'ant-btn-loading');

    cy.getTestId(tableName).find('table').find('.ant-table-tbody')
      .children('.ant-table-row').first().find('td').first()
      .should('include.text', text ?? value);
    cy.getTestId(btnTestId ?? 'search_btn')
      .should('not.have.class', 'ant-btn-loading');

    cy.getTestId(tableName).siblings('.ant-pagination').find('li[title="1"]')
      .should('have.class', 'ant-pagination-item-active');
  };
}

export function tableBtnClick(tableName: string, index: number) {
  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').first().find('td').last().children().eq(index)
    .click();
}

export function validateModalBtnGroup(modalName: string) {
  cy.getTestId(modalName).getTestId('modal_btn_group').find('.ant-btn').first()
    .should('have.class', 'ant-btn-loading');
  cy.getTestId(modalName).getTestId('modal_btn_group').find('.ant-btn').last()
    .should('have.attr', 'disabled');
}

export function validatePut(
  modalName: string,
  label: string,
  options?: {addBtnTestId?: string},
) {
  const {addBtnTestId} = options ?? {};

  function validateAdd(fn: () => void) {
    cy.getTestId(addBtnTestId ?? 'add_btn').click();

    cy.getTestId(modalName).should('exist').and('be.visible');
    cy.getTestId(modalName).find('h3')
      .should('include.text', `新增${label}`);

    fn();

    cy.getTestId(modalName).find('form').submit();

    validateModalBtnGroup(modalName);

    cy.get('.ant-message-notice-content').should('include.text', '新增成功');
    cy.getTestId(modalName).should('not.exist');
  }

  function validateEdit(tableName: string, validateFn: () => void) {
    tableBtnClick(tableName, 0);

    cy.getTestId(modalName).should('exist').and('be.visible');
    cy.getTestId(modalName).find('h3').should('include.text', `修改${label}`);

    validateFn();

    cy.getTestId(modalName).find('form').submit();

    validateModalBtnGroup(modalName);

    cy.get('.ant-message-notice-content').should('include.text', '修改成功');
    cy.getTestId(modalName).should('not.exist');
  }

  return {validateAdd, validateEdit};
}

export function validateDelete(
  tableName: string,
  label: string,
) {
  tableBtnClick(tableName, 1);

  cy.get('.ant-modal-content').should('be.visible');

  cy.get('.ant-modal-confirm-title').should('include.text', `删除${label}`);
  cy.get('.ant-modal-confirm-content').should(
    'include.text',
    `你确定要删除当前${label}吗？`,
  );

  cy.get('.ant-modal-confirm-btns').children().last().trigger('click');

  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').first().find('td').last().children().eq(1)
    .should('have.class', 'ant-btn-loading');

  cy.getTestId(tableName).find('table').find('.ant-table-tbody')
    .children('.ant-table-row').first().find('td').last().children().first()
    .should('have.attr', 'disabled');

  cy.get('.ant-message-notice-content').should('include.text', '删除成功');
}

export function validateExport() {
  cy.getTestId('export_btn').click();

  cy.getTestId('export_btn').should('have.class', 'ant-btn-loading');

  cy.wait('@export');

  cy.getTestId('export_btn').should('not.have.class', 'ant-btn-loading');
}
