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/tracks/tracksSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



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

    

    

    

    

    

    

    

    

    

    

    
    release: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    track_number: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'version': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    genre: null,
    

    
    
    
    'isrc_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    isrc_source: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    audio_file: [],
    

    

    

    
    
    

    

    

    
    duration_seconds: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sample_rate_hz: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    bitrate_kbps: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    audio_format: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    explicit_content: false,
    

    

    

    

    

    

    
    
    

    
    lyrics: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'audio_fingerprint': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    quality_status: '',
    

    

    

    

    
    
    

    
    quality_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

  const { tracks } = useAppSelector((state) => state.tracks)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { tracksId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Release' labelFor='release'>
        <Field
            name='release'
            id='release'
            component={SelectField}
            options={initialValues.release}
            itemRef={'releases'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Tracktitle"
    >
        <Field
            name="title"
            placeholder="Tracktitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Tracknumber"
    >
        <Field
            type="number"
            name="track_number"
            placeholder="Tracknumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Version"
    >
        <Field
            name="version"
            placeholder="Version"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Genre' labelFor='genre'>
        <Field
            name='genre'
            id='genre'
            component={SelectField}
            options={initialValues.genre}
            itemRef={'genres'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="ISRCcode"
    >
        <Field
            name="isrc_code"
            placeholder="ISRCcode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ISRCsource" labelFor="isrc_source">
        <Field name="isrc_source" id="isrc_source" component="select">
          
            <option value="auto">auto</option>
          
            <option value="manual">manual</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='Audiofile'
            color='info'
            icon={mdiUpload}
            path={'tracks/audio_file'}
            name='audio_file'
            id='audio_file'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  
    <FormField
        label="Durationseconds"
    >
        <Field
            type="number"
            name="duration_seconds"
            placeholder="Durationseconds"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SamplerateHz"
    >
        <Field
            type="number"
            name="sample_rate_hz"
            placeholder="SamplerateHz"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Bitratekbps"
    >
        <Field
            type="number"
            name="bitrate_kbps"
            placeholder="Bitratekbps"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Audioformat" labelFor="audio_format">
        <Field name="audio_format" id="audio_format" component="select">
          
            <option value="wav">wav</option>
          
            <option value="flac">flac</option>
          
            <option value="mp3">mp3</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Explicitcontent' labelFor='explicit_content'>
        <Field
            name='explicit_content'
            id='explicit_content'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="Lyrics" hasTextareaHeight>
        <Field name="lyrics" as="textarea" placeholder="Lyrics" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Audiofingerprint"
    >
        <Field
            name="audio_fingerprint"
            placeholder="Audiofingerprint"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Qualitystatus" labelFor="quality_status">
        <Field name="quality_status" id="quality_status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="passed">passed</option>
          
            <option value="failed">failed</option>
          
            <option value="needs_attention">needs_attention</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Qualitynotes" hasTextareaHeight>
        <Field name="quality_notes" as="textarea" placeholder="Qualitynotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='organizations' labelFor='organizations'>
        <Field
            name='organizations'
            id='organizations'
            component={SelectField}
            options={initialValues.organizations}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            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('/tracks/tracks-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTracks
