'use client';

import { ModelIcon } from '@lobehub/icons';
import { Button } from 'antd';
import { createStyles } from 'antd-style';
import dayjs from 'dayjs';
import Link from 'next/link';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import { DiscoverModelItem } from '@/types/discover';

import ModelFeatureTags from '../../../../features/ModelFeatureTags';
import Back from '../../../features/Back';
import { useRouter } from 'next/navigation';
import { useUserStore } from '@/store/user';

export const useStyles = createStyles(({ css, token }) => ({
  conTitle: css`
    width: 125px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #fff;
    font-size: 20px;
    border-radius: 0px 129px 129px 0px;
    opacity: 1;
    background: linear-gradient(270deg, #2D65FF 0%, rgba(0, 166, 255, 0.52) 99%);
  `,
  description: css`
    padding: 0 45px;
    margin-top: 10px;
  `,
  // tag: css`
  //   color: ${token.colorTextSecondary};
  //   background: ${token.colorFillSecondary};
  //   border: none;
  // `,
  tag: css`
    padding: 0px 45px;
    margin-bottom: 10px;
  `,
  time: css`
    font-size: 12px;
    color: ${token.colorTextDescription};
  `,
  title: css`
    margin-block: 0;
    font-size: 18px;
    font-weight: bold;
    line-height: 1.2;
  `,
  top: css`
    padding: 20px 0px;
    box-sizing: border-box;
    border-width: 0px 0px 2px 0px;
    border-style: solid;
    border-color: rgba(187, 204, 253, 0.24);
    padding: 20px 50px;
  `,
  topBtn: css`
    border-radius: 20px;
    color: #2E62FF;
    border: 1px solid #2E62FF;
  `,
}));

interface HeaderProps {
  data: DiscoverModelItem;
  identifier: string;
  mobile?: boolean;
}
const getUserId = (s: { user?: { id?: any } }) => s.user?.id;
const Header = memo<HeaderProps>(({ identifier, data, mobile }) => {
  const { styles, theme } = useStyles();
  const router = useRouter()
  const { t } = useTranslation(['discover', 'models']);
  const userId = getUserId(useUserStore.getState())
  const handleBack = () => {
    router.push('/discover/models?userid=' + userId)
  }
  return (
    <Flexbox gap={12} width={'100%'}>
      <Flexbox align={'center'} className={styles.top} gap={8} horizontal justify={'space-between'} width={'100%'}>
        <Flexbox align={'center'} gap={16} horizontal justify={'flex-start'}>
          <ModelIcon model={identifier} size={48} type={'avatar'} />
          <Flexbox gap={2}>
            <h1 className={styles.title}>{data.meta.title}</h1>
            <Flexbox
              align={'center'}
              gap={12}
              horizontal
              style={{ color: theme.colorTextSecondary }}
            >
              <div>{identifier}</div>
              <time className={styles.time} dateTime={new Date(data.createdAt).toISOString()}>
                {data.createdAt}
              </time>
            </Flexbox>
          </Flexbox>
        </Flexbox>
        {!mobile && (
          <Flexbox align={'center'} gap={4} horizontal justify={'flex-end'}>
            <Button className={styles.topBtn} onClick={() => handleBack()}>返回列表</Button>
          </Flexbox>
        )}
      </Flexbox>
      <div className={styles.conTitle}>简介</div>
      {data.meta.description && <div className={styles.description}>{t(`${identifier}.description`, { ns: 'models' })}</div>}
      <div className={styles.tag}>
        <ModelFeatureTags
          functionCall={data.meta.functionCall}
          tokens={data.meta.tokens}
          vision={data.meta.vision}
        />
      </div>
      <div className={styles.conTitle}>参数</div>
    </Flexbox>
  );
});

export default Header;
