import {
  DEMAND_ADVERTISER_PAGE,
  DEMAND_TAG_FORMAT,
  DEMAND_TAG_FREQUENCY_TAB_STATUS,
  DEMAND_TAG_HB_GAM_PLATFORMS,
  DEMAND_TAG_KEY_VALUE_TARGETING_STATUS,
  DEMAND_TAG_PAGE,
  DEMAND_TAG_PAGE_TYPE_VALUES,
  SUPPLY_TAG_PAGE,
} from '../../constants';
import { DEMAND_ADVERTISER_SETTINGS_VALIDATION_REDUX_FIELD } from '@/modules/marketplace/account/components/AdvertiserSettings/constants';

import validation from '../../helpers/demandTagFormValidationRules';
import { isFrequencyCapHasData } from '../../helpers/isFrequencyCapHasData';
import { slice } from '../slices';
import createFormSelector from '@/modules/@common/Form/redux/selectors';

const nameSpace = slice.name;

export const pageConfigSelector = (state$) => state$[nameSpace].pageConfig;
export const activeTabIndexSelector = (state$) => state$[nameSpace].activeTabIndex;
export const filterByDateSelector = (state$) => state$[nameSpace].filterByDate;
export const filterByStatusSelector = (state$) => state$[nameSpace].filterByStatus;
export const filterByCountrySelector = (state$) => state$[nameSpace].filterByCountry;
export const filterByDeviceSelector = (state$) => state$[nameSpace].filterByDevice;
export const filterByPlayerSelector = (state$) => state$[nameSpace].filterByPlayer;
export const filterByPlayerOptionsSelector = (state$) => state$[nameSpace].filterByPlayerOptions;
export const filterByLabelSelector = (state$) => state$[nameSpace].filterByLabel;
export const labelsOptionsSelector = (state$) => state$[nameSpace].labelsOptions;
export const infoSelector = (state$) => state$[nameSpace].info;
export const totalSelector = (state$) => state$[nameSpace].total;
export const dataSelector = (state$) => state$[nameSpace].data;
export const totalCountSelector = (state$) => state$[nameSpace].totalCount;
export const pageSelector = (state$) => state$[nameSpace].page;
export const rowsPerPageSelector = (state$) => state$[nameSpace].rowsPerPage;
export const searchSelector = (state$) => state$[nameSpace].search;
export const sortBySelector = (state$) => state$[nameSpace].sortBy;
export const sortOrderSelector = (state$) => state$[nameSpace].sortOrder;
export const isLoadingSelector = (state$) => state$[nameSpace].isLoading;
export const chartTabSelector = (state$) => state$[nameSpace].chartTab;
export const chartDataSelector = (state$) => state$[nameSpace].chartData;
export const chartHistorySelector = (state$) => state$[nameSpace].chartHistory;
export const isChartLoadingSelector = (state$) => state$[nameSpace].isChartLoading;
export const timeIntervalFilterSelector = (state$) => state$[nameSpace].timeIntervalFilter;
export const comparisonFilterSelector = (state$) => state$[nameSpace].comparisonFilter;
export const userHubsSelector = (state$) => state$[nameSpace].userHubs;
export const userDemandAccountsSelector = (state$) => state$[nameSpace].userDemandAccounts;
export const demandAccountInfoSelector = (state$) => state$[nameSpace].demandAccountInfo;
export const modalSelector = (state$) => state$[nameSpace].modal;
export const supplySelector = (state$) => state$[nameSpace].supply;
export const automaticOptimizationSelector = (state$) => state$[nameSpace].automaticOptimization;
export const budgetingSelector = (state$) => state$[nameSpace].budgeting;
export const frequencyCapSelector = (state$) => state$[nameSpace].frequencyCap;
export const pricingSelector = (state$) => state$[nameSpace].pricing;
export const settingsSelector = (state$) => state$[nameSpace].settings;
export const targetingSelector = (state$) => state$[nameSpace].targeting;
export const advertiserSettingsSelector = (state$) => state$[nameSpace].advertiserSettings;

export const isDemandTagFormSubmitDisabled = ($state) => {
  const settings = settingsSelector($state);
  const pricing = pricingSelector($state);
  const frequencyCap = frequencyCapSelector($state);
  const targeting = targetingSelector($state);
  const isLoading = isLoadingSelector($state);

  return !(
    settings.name &&
    Number.isInteger(settings.defaultTier) &&
    pricing.model &&
    (pricing.fixedRpmRateTable.rows.length ||
      (settings.type === DEMAND_TAG_PAGE_TYPE_VALUES.hb &&
        !DEMAND_TAG_HB_GAM_PLATFORMS.includes(settings.platform?.code))) &&
    (settings.type === DEMAND_TAG_PAGE_TYPE_VALUES.tag ? settings.url : true) &&
    validation.validateTimeout(settings.timeout).isValid &&
    (frequencyCap.status === DEMAND_TAG_FREQUENCY_TAB_STATUS.active ? isFrequencyCapHasData(frequencyCap) : true) &&
    (pricing.publisherDemand ? !!pricing.adServingFeesTable.rows.length : true) &&
    (targeting.kvTargetingStatus === DEMAND_TAG_KEY_VALUE_TARGETING_STATUS.enabled
      ? targeting.kvTargeting?.every((item) => !!item?.keyName && !!(item?.valueLists?.length || item?.values?.length))
      : true) &&
    !isLoading
  );
};

export const isSupplyTagFormSubmitDisabled = ($state) => {
  const supply = supplySelector($state);
  const pageConfig = pageConfigSelector($state);
  const isLoading = isLoadingSelector($state);

  return !(
    supply.name &&
    (pageConfig.isCreatingNew ? supply.pricing.length : true) &&
    (supply.format === DEMAND_TAG_FORMAT.display ? supply.displayType : true) &&
    !isLoading
  );
};

export const isFormSubmitDisabledSelector = ($state) => {
  const pageConfig = pageConfigSelector($state);

  if (pageConfig && pageConfig.type === DEMAND_TAG_PAGE) {
    return isDemandTagFormSubmitDisabled($state);
  }

  if (pageConfig && pageConfig.type === SUPPLY_TAG_PAGE) {
    return isSupplyTagFormSubmitDisabled($state);
  }

  if (pageConfig && pageConfig.type === DEMAND_ADVERTISER_PAGE) {
    return false;
  }

  return true;
};

const advertiserSettingsValidationSelector = createFormSelector(
  DEMAND_ADVERTISER_SETTINGS_VALIDATION_REDUX_FIELD,
  nameSpace,
);

export const advertiserSettingsValidationScrollFieldSelector = (state) =>
  advertiserSettingsValidationSelector.getScrollField(state);
export const advertiserSettingsValidationSchemeSelector = (state) =>
  advertiserSettingsValidationSelector.schemeSelector(state);
export const fullAccessToStoreFieldsForValidation = (state) => state[nameSpace];
