import { FieldDataType, getColumns, ModelType } from '@gadmin2/react-common';
import {
  CloneButton,
  DeleteButton,
  EditButton,
  ListButton,
  RefreshButton,
  Show,
} from '@refinedev/antd';
import {
  IResourceComponentsProps,
  useShow,
  useTranslate,
} from '@refinedev/core';
import { modelsMap } from 'generated/models.index';
import produce from 'immer';
import React from 'react';
import {
  gameShowConfig as showConfig,
  gameShowPrismaSelect as showPrismaSelect,
} from '../../generated/props/game/config';
import { gameModel } from '../../generated/props/game/model';
import type { Game } from '../../generated/types/prisma.types'; //'.prisma/client/index';
import { getShowField } from '../../helpers';

const model: ModelType = produce(gameModel, (draft: FieldDataType) => {
  /*
  const publishedField = draft["status"] || {};
  publishedField.render = (value: any) => {
    return (<BooleanField
      value={value === "1"}
      trueIcon={<CheckCircleTwoTone twoToneColor="#52c41a" />}
      falseIcon={<CloseCircleTwoTone twoToneColor="red" />}
    />)
  };
  */
});

const fields = getColumns('Game', showConfig.fields, {
  ...modelsMap,
  Game: { model, meta: modelsMap['Game'].meta },
});

export const GameShow: React.FC<IResourceComponentsProps> = () => {
  const resourceName = 'game';
  const t = useTranslate();

  const { queryResult } = useShow<Game>({
    meta: { select: showPrismaSelect },
  });
  const { data, isLoading } = queryResult;
  const record = data?.data;

  return (
    <Show
      isLoading={isLoading}
      headerButtons={
        <>
          <ListButton />
          <EditButton recordItemId={record?.id} />
          <CloneButton recordItemId={record?.id} />
          <DeleteButton recordItemId={record?.id} />
          <RefreshButton
            recordItemId={record?.id}
            meta={{ select: showPrismaSelect }}
          />
        </>
      }
    >
      {fields.map((field: any) => {
        return getShowField(field, resourceName, record, t, model, modelsMap);
      })}
    </Show>
  );
};
