import { useMemo } from 'react';

import { Type, TypeResource } from '@/back-api';
import { useServiceContext } from '@/service/ServiceContext';
import { SessionServiceProps } from '@/service/session';
import { DataStoreType, useDataStore } from '@/utils/data-store';
import { DataTools, TypeCheck } from '@/utils/data-tools';
import { isNullOrUndefined } from '@/utils/validator';

import { useMediaService } from './Media';

export type TypeServiceProps = {
  store: DataStoreType<Type>;
};

export const useTypeService = (): TypeServiceProps => {
  const { type } = useServiceContext();
  return type;
};

export const useTypeServiceWrapped = (
  session: SessionServiceProps
): TypeServiceProps => {
  const store = useDataStore<Type>(
    {
      restApiName: 'Type',
      primaryKey: 'id',
      available: session.token !== undefined,
      getsCall: () => {
        return TypeResource.gets({
          restConfig: session.getRestConfig(),
        });
      },
    },
    [session.token]
  );

  return { store };
};

export const useOrderedTypes = (titleFilter?: string) => {
  const { store } = useTypeService();
  const dataTypes = useMemo(() => {
    let tmpData = store.data;
    if (!isNullOrUndefined(titleFilter)) {
      tmpData = DataTools.getNameLike(tmpData, titleFilter);
    }
    return DataTools.getsWhere(
      tmpData,
      [
        {
          check: TypeCheck.NOT_EQUAL,
          key: 'id',
          value: [undefined, null],
        },
      ],
      ['name', 'id']
    );
  }, [store.data, titleFilter]);
  return { isLoading: store.isLoading, dataTypes };
};
export const useSpecificType = (id: number | undefined) => {
  const { store } = useTypeService();
  const dataType = useMemo(() => {
    return store.get(id);
  }, [store.data, id]);
  return { dataType };
};

export const useTypeCountVideo = (id?: number) => {
  const { store } = useMediaService();
  const countVideoWithType = useMemo(() => {
    if (id === undefined) {
      return 0;
    }
    return DataTools.count(store.data, [
      {
        check: TypeCheck.EQUAL,
        key: 'typeId',
        value: id,
      },
    ]);
  }, [store.data, id]);
  return { isLoading: store.isLoading, countVideoWithType };
};

export const useTypeGetVideo = (id?: number) => {
  const { store } = useMediaService();
  const videoWithType = useMemo(() => {
    if (id === undefined) {
      return [];
    }
    return DataTools.getsWhere(
      store.data,
      [
        {
          check: TypeCheck.EQUAL,
          key: 'typeId',
          value: id,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'seriesId',
          value: undefined,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'universeId',
          value: undefined,
        },
      ],
      ['name']
    );
  }, [store.data, id]);
  return { isLoading: store.isLoading, videoWithType };
};

export const useTypeSeriesGetVideo = (idType?: number, idSeries?: number) => {
  const { store } = useMediaService();
  const videoWithType = useMemo(() => {
    if (idType === undefined) {
      return [];
    }
    if (idSeries === undefined) {
      return [];
    }
    return DataTools.getsWhere(
      store.data,
      [
        {
          check: TypeCheck.EQUAL,
          key: 'typeId',
          value: idType,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'seriesId',
          value: idSeries,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'seasonId',
          value: undefined,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'universeId',
          value: undefined,
        },
      ],
      ['name']
    );
  }, [store.data, idType]);
  return { isLoading: store.isLoading, videoWithType };
};

export const useTypeSeriesSeasonGetVideo = (idType?: number, idSeries?: number, idSeason?: number) => {
  const { store } = useMediaService();
  const videoWithType = useMemo(() => {
    if (idType === undefined) {
      return [];
    }
    if (idSeries === undefined) {
      return [];
    }
    if (idSeason === undefined) {
      return [];
    }
    return DataTools.getsWhere(
      store.data,
      [
        {
          check: TypeCheck.EQUAL,
          key: 'typeId',
          value: idType,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'seriesId',
          value: idSeries,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'seasonId',
          value: idSeason,
        },
        {
          check: TypeCheck.EQUAL,
          key: 'universeId',
          value: undefined,
        },
      ],
      ['episode', 'name']
    );
  }, [store.data, idType]);
  return { isLoading: store.isLoading, videoWithType };
};
