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/assets/assetsSlice'
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 EditAssets = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    workspace: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    uploaded_user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    project: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    song: null,
    

    
    
    

    

    

    

    

    

    

    

    
    asset_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    audio_role: '',
    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    file_blobs: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    image_blobs: [],
    

    

    
    
    
    'duration_seconds': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sample_rate_hz: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    bit_depth: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_stereo: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { assetsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Workspace' labelFor='workspace'>
        <Field
            name='workspace'
            id='workspace'
            component={SelectField}
            options={initialValues.workspace}
            itemRef={'workspaces'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='UploadedBy' labelFor='uploaded_user'>
        <Field
            name='uploaded_user'
            id='uploaded_user'
            component={SelectField}
            options={initialValues.uploaded_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Project' labelFor='project'>
        <Field
            name='project'
            id='project'
            component={SelectField}
            options={initialValues.project}
            itemRef={'projects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Song' labelFor='song'>
        <Field
            name='song'
            id='song'
            component={SelectField}
            options={initialValues.song}
            itemRef={'songs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AssetType" labelFor="asset_type">
        <Field name="asset_type" id="asset_type" component="select">
          
            <option value="audio">audio</option>
          
            <option value="midi">midi</option>
          
            <option value="image">image</option>
          
            <option value="document">document</option>
          
            <option value="preset">preset</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AudioRole" labelFor="audio_role">
        <Field name="audio_role" id="audio_role" component="select">
          
            <option value="vocal_raw">vocal_raw</option>
          
            <option value="vocal_processed">vocal_processed</option>
          
            <option value="instrumental">instrumental</option>
          
            <option value="drums">drums</option>
          
            <option value="bass">bass</option>
          
            <option value="instruments">instruments</option>
          
            <option value="mix">mix</option>
          
            <option value="master">master</option>
          
            <option value="stem">stem</option>
          
            <option value="reference">reference</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="AssetName"
    >
        <Field
            name="name"
            placeholder="AssetName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Images'
                color='info'
                icon={mdiUpload}
                path={'assets/image_blobs'}
                name='image_blobs'
                id='image_blobs'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></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="BitDepth"
    >
        <Field
            type="number"
            name="bit_depth"
            placeholder="BitDepth"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsStereo' labelFor='is_stereo'>
        <Field
            name='is_stereo'
            id='is_stereo'
            component={SwitchField}
        ></Field>
    </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('/assets/assets-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAssets
