import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";

import CardBox from '../../components/CardBox'
import LayoutAuthenticated from '../../layouts/Authenticated'
import SectionMain from '../../components/SectionMain'
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton'
import { getPageTitle } from '../../config'

import { Field, Form, Formik } from 'formik'
import FormField from '../../components/FormField'
import BaseDivider from '../../components/BaseDivider'
import BaseButtons from '../../components/BaseButtons'
import BaseButton from '../../components/BaseButton'
import FormCheckRadio from '../../components/FormCheckRadio'
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup'
import FormFilePicker from '../../components/FormFilePicker'
import FormImagePicker from '../../components/FormImagePicker'
import { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/beatmaps/beatmapsSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";



const EditBeatmaps = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    
    osu_beatmap_numeric: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'difficulty_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    mode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    
    playcount: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    passcount: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'star_rating': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bpm': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    length_seconds: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'cs': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ar': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'od': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'hp': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    last_synced_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    beatmap_set: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    mapper: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    artist: null,
    

    
    
    
  }
  const [initialValues, setInitialValues] = useState(initVals)

  const { beatmaps } = useAppSelector((state) => state.beatmaps)
  

  const { beatmapsId } = router.query

  useEffect(() => {
    dispatch(fetch({ id: beatmapsId }))
  }, [beatmapsId])

  useEffect(() => {
    if (typeof beatmaps === 'object') {
      setInitialValues(beatmaps)
    }
  }, [beatmaps])

  useEffect(() => {
      if (typeof beatmaps === 'object') {

          const newInitialVal = {...initVals};

          Object.keys(initVals).forEach(el => newInitialVal[el] = (beatmaps)[el])

          setInitialValues(newInitialVal);
      }
  }, [beatmaps])

  const handleSubmit = async (data) => {
    await dispatch(update({ id: beatmapsId, data }))
    await router.push('/beatmaps/beatmaps-list')
  }

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit beatmaps')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit beatmaps'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  
    <FormField
        label="OsuBeatmapNumeric"
    >
        <Field
            type="number"
            name="osu_beatmap_numeric"
            placeholder="OsuBeatmapNumeric"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DifficultyName"
    >
        <Field
            name="difficulty_name"
            placeholder="DifficultyName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Mode" labelFor="mode">
        <Field name="mode" id="mode" component="select">
          
            <option value="osu">osu</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="ranked">ranked</option>
          
            <option value="loved">loved</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Playcount"
    >
        <Field
            type="number"
            name="playcount"
            placeholder="Playcount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Passcount"
    >
        <Field
            type="number"
            name="passcount"
            placeholder="Passcount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="StarRating"
    >
        <Field
            type="number"
            name="star_rating"
            placeholder="StarRating"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Bpm"
    >
        <Field
            type="number"
            name="bpm"
            placeholder="Bpm"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LengthSeconds"
    >
        <Field
            type="number"
            name="length_seconds"
            placeholder="LengthSeconds"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Cs"
    >
        <Field
            type="number"
            name="cs"
            placeholder="Cs"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Ar"
    >
        <Field
            type="number"
            name="ar"
            placeholder="Ar"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Od"
    >
        <Field
            type="number"
            name="od"
            placeholder="Od"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Hp"
    >
        <Field
            type="number"
            name="hp"
            placeholder="Hp"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="LastSyncedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.last_synced_at ?
                  new Date(
                      dayjs(initialValues.last_synced_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'last_synced_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='BeatmapSet' labelFor='beatmap_set'>
        <Field
            name='beatmap_set'
            id='beatmap_set'
            component={SelectField}
            options={initialValues.beatmap_set}
            itemRef={'beatmap_sets'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Mapper' labelFor='mapper'>
        <Field
            name='mapper'
            id='mapper'
            component={SelectField}
            options={initialValues.mapper}
            itemRef={'mappers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'osu_username'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Artist' labelFor='artist'>
        <Field
            name='artist'
            id='artist'
            component={SelectField}
            options={initialValues.artist}
            itemRef={'artists'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  


  

              <BaseDivider />
              <BaseButtons>
                <BaseButton type="submit" color="info" label="Submit" />
                <BaseButton type="reset" color="info" outline label="Reset" />
                <BaseButton type='reset' color='danger' outline label='Cancel' onClick={() => router.push('/beatmaps/beatmaps-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

EditBeatmaps.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated
        
          permission={'UPDATE_BEATMAPS'}
        
      >
          {page}
      </LayoutAuthenticated>
  )
}

export default EditBeatmaps
