import React from 'react'
import { ImageUploadField } from '../ImageUploadField'
import { Button, Card, ErrorState, Input, Textarea } from '../ui'
import { t } from '@/lib/i18n'
import type { SeriesDraft } from './createSeriesModalTypes'
import type { CreateSeriesModalController } from './useCreateSeriesModalController'

export function CreateSeriesModalView({ ctrl }: { ctrl: CreateSeriesModalController }): React.ReactElement {
  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
      <Card variant="default" className="bg-cyber-dark max-w-2xl w-full mx-4 max-h-[90vh] overflow-y-auto">
        <CreateSeriesModalHeader loading={ctrl.loading} onClose={ctrl.handleClose} />
        {!ctrl.canPublish ? <NotAuthorWarning /> : null}
        <CreateSeriesForm ctrl={ctrl} />
      </Card>
    </div>
  )
}

function CreateSeriesModalHeader({ loading, onClose }: { loading: boolean; onClose: () => void }): React.ReactElement {
  return (
    <div className="flex items-center justify-between mb-6">
      <h2 className="text-2xl font-semibold text-neon-cyan">{t('series.create.title')}</h2>
      <Button
        type="button"
        variant="ghost"
        size="small"
        onClick={onClose}
        disabled={loading}
        className="text-cyber-accent hover:text-neon-cyan p-0 min-w-0"
        aria-label={t('common.close')}
      >
        ✕
      </Button>
    </div>
  )
}

function NotAuthorWarning(): React.ReactElement {
  return (
    <Card variant="default" className="mb-4 p-4 bg-yellow-900/30 border-yellow-500/50 text-yellow-300">
      <p>{t('series.create.error.notAuthor')}</p>
    </Card>
  )
}

function CreateSeriesForm({ ctrl }: { ctrl: CreateSeriesModalController }): React.ReactElement {
  return (
    <form onSubmit={(e) => void ctrl.handleSubmit(e)} className="space-y-4">
      <SeriesTextFields draft={ctrl.draft} setDraft={ctrl.setDraft} loading={ctrl.loading} canPublish={ctrl.canPublish} />
      <SeriesCategoryField draft={ctrl.draft} setDraft={ctrl.setDraft} loading={ctrl.loading} canPublish={ctrl.canPublish} />
      <SeriesCoverField draft={ctrl.draft} setDraft={ctrl.setDraft} />
      <SeriesError error={ctrl.error} />
      <SeriesActions loading={ctrl.loading} canPublish={ctrl.canPublish} onClose={ctrl.handleClose} />
    </form>
  )
}

function SeriesTextFields(params: {
  draft: SeriesDraft
  setDraft: (draft: SeriesDraft) => void
  loading: boolean
  canPublish: boolean
}): React.ReactElement {
  const disabled = params.loading || !params.canPublish
  return (
    <>
      <TextField
        id="series-title"
        label={t('series.create.field.title')}
        value={params.draft.title}
        disabled={disabled}
        required
        onChange={(value) => params.setDraft({ ...params.draft, title: value })}
      />
      <TextAreaField
        id="series-description"
        label={t('series.create.field.description')}
        value={params.draft.description}
        disabled={disabled}
        required
        rows={4}
        onChange={(value) => params.setDraft({ ...params.draft, description: value })}
      />
      <TextAreaField
        id="series-preview"
        label={t('series.create.field.preview')}
        value={params.draft.preview}
        disabled={disabled}
        required
        rows={3}
        helpText={t('series.create.field.preview.help')}
        onChange={(value) => params.setDraft({ ...params.draft, preview: value })}
      />
    </>
  )
}

function SeriesCategoryField(params: {
  draft: SeriesDraft
  setDraft: (draft: SeriesDraft) => void
  loading: boolean
  canPublish: boolean
}): React.ReactElement {
  const disabled = params.loading || !params.canPublish
  return (
    <div>
      <label htmlFor="series-category" className="block text-sm font-medium text-neon-cyan mb-2">
        {t('series.create.field.category')}
      </label>
      <select
        id="series-category"
        value={params.draft.category}
        onChange={(e) => params.setDraft({ ...params.draft, category: e.target.value as SeriesDraft['category'] })}
        className="w-full bg-cyber-darker border-cyber-accent/30 text-cyber-light"
        required
        disabled={disabled}
      >
        <option value="science-fiction">{t('category.science-fiction')}</option>
        <option value="scientific-research">{t('category.scientific-research')}</option>
      </select>
    </div>
  )
}

function SeriesCoverField({ draft, setDraft }: { draft: SeriesDraft; setDraft: (draft: SeriesDraft) => void }): React.ReactElement {
  return (
    <ImageUploadField
      id="series-cover"
      label={t('series.create.field.cover')}
      value={draft.coverUrl}
      onChange={(url) => setDraft({ ...draft, coverUrl: url })}
      helpText={t('series.create.field.cover.help')}
    />
  )
}

function SeriesError({ error }: { error: string | null }): React.ReactElement | null {
  if (!error) {
    return null
  }
  return <ErrorState message={error} />
}

function SeriesActions(params: { loading: boolean; canPublish: boolean; onClose: () => void }): React.ReactElement {
  return (
    <div className="flex items-center justify-end gap-4 pt-4">
      <Button
        type="button"
        variant="ghost"
        onClick={params.onClose}
        disabled={params.loading}
      >
        {t('common.cancel')}
      </Button>
      <Button
        type="submit"
        variant="primary"
        disabled={params.loading || !params.canPublish}
        loading={params.loading}
      >
        {params.loading ? t('common.loading') : t('series.create.submit')}
      </Button>
    </div>
  )
}

function TextField(params: {
  id: string
  label: string
  value: string
  disabled: boolean
  required: boolean
  onChange: (value: string) => void
}): React.ReactElement {
  return (
    <Input
      id={params.id}
      type="text"
      label={params.label}
      value={params.value}
      onChange={(e) => params.onChange(e.target.value)}
      required={params.required}
      disabled={params.disabled}
    />
  )
}

function TextAreaField(params: {
  id: string
  label: string
  value: string
  disabled: boolean
  required: boolean
  rows: number
  helpText?: string
  onChange: (value: string) => void
}): React.ReactElement {
  return (
    <Textarea
      id={params.id}
      label={params.label}
      value={params.value}
      onChange={(e) => params.onChange(e.target.value)}
      rows={params.rows}
      required={params.required}
      disabled={params.disabled}
      {...(params.helpText ? { helperText: params.helpText } : {})}
    />
  )
}
