import { CurrentUserMark } from '../../types/mark.ts'
import styles from './style.module.scss'

import { MARK_TYPE_TRANSLATION, NEXT, SIGN } from '../../utils/const.ts'
import {
  findNextIncompleteCurrentUserMark,
  isCurrentUserMarksComplete,
  isCurrentValueLast
} from '../../utils'
import React, { useState } from 'react'

interface MarkFormFieldProps {
  currentUserMarks: CurrentUserMark[]
  handleCurrentUserMarkChange: (mark: CurrentUserMark) => void
  handleSelectedMarkValueChange: (
    event: React.ChangeEvent<HTMLInputElement>
  ) => void
  handleSubmit: (event: React.FormEvent<HTMLFormElement>) => void
  selectedMark: CurrentUserMark
  selectedMarkValue: string
}

/**
 * Responsible for rendering a form field connected to a mark and keeping track of its value.
 */
const MarkFormField = ({
  handleSubmit,
  handleSelectedMarkValueChange,
  selectedMark,
  selectedMarkValue,
  currentUserMarks,
  handleCurrentUserMarkChange
}: MarkFormFieldProps) => {
  const [displayActions, setDisplayActions] = useState(true)
  const getSubmitButtonText = () => (isReadyToSign() ? SIGN : NEXT)
  const isReadyToSign = () =>
    isCurrentUserMarksComplete(currentUserMarks) ||
    isCurrentValueLast(currentUserMarks, selectedMark, selectedMarkValue)
  const isCurrent = (currentMark: CurrentUserMark) =>
    currentMark.id === selectedMark.id
  const isDone = (currentMark: CurrentUserMark) =>
    isCurrent(currentMark) ? !!selectedMarkValue : currentMark.isCompleted
  const findNext = () => {
    return (
      currentUserMarks[selectedMark.id] ||
      findNextIncompleteCurrentUserMark(currentUserMarks)
    )
  }
  const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    console.log('handle form submit runs...')
    return isReadyToSign()
      ? handleSubmit(event)
      : handleCurrentUserMarkChange(findNext()!)
  }
  const toggleActions = () => setDisplayActions(!displayActions)
  return (
    <div className={styles.container}>
      <div className={styles.trigger}>
        <button
          onClick={toggleActions}
          className={styles.triggerBtn}
          type="button"
        >
          <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox="-64 0 512 512"
            width="1em"
            height="1em"
            fill="currentColor"
            transform={displayActions ? 'rotate(180)' : 'rotate(0)'}
          >
            <path d="M352 352c-8.188 0-16.38-3.125-22.62-9.375L192 205.3l-137.4 137.4c-12.5 12.5-32.75 12.5-45.25 0s-12.5-32.75 0-45.25l160-160c12.5-12.5 32.75-12.5 45.25 0l160 160c12.5 12.5 12.5 32.75 0 45.25C368.4 348.9 360.2 352 352 352z"></path>
          </svg>
        </button>
      </div>
      <div className={`${styles.actions} ${displayActions && styles.expanded}`}>
        <div className={styles.actionsWrapper}>
          <div className={styles.actionsTop}>
            <div className={styles.actionsTopInfo}>
              <p className={styles.actionsTopInfoText}>Add your signature</p>
            </div>
          </div>
          <div className={styles.inputWrapper}>
            <form onSubmit={(e) => handleFormSubmit(e)}>
              <input
                className={styles.input}
                placeholder={
                  MARK_TYPE_TRANSLATION[selectedMark.mark.type.valueOf()]
                }
                onChange={handleSelectedMarkValueChange}
                value={selectedMarkValue}
              />
              <div className={styles.actionsBottom}>
                <button type="submit" className={styles.submitButton}>
                  {getSubmitButtonText()}
                </button>
              </div>
            </form>
            <div className={styles.footerContainer}>
              <div className={styles.footer}>
                {currentUserMarks.map((mark, index) => {
                  return (
                    <div className={styles.pagination} key={index}>
                      <button
                        className={`${styles.paginationButton} ${isDone(mark) && styles.paginationButtonDone}`}
                        onClick={() => handleCurrentUserMarkChange(mark)}
                      >
                        {mark.id}
                      </button>
                      {isCurrent(mark) && (
                        <div className={styles.paginationButtonCurrent}></div>
                      )}
                    </div>
                  )
                })}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  )
}

export default MarkFormField
