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/voice_sessions/voice_sessionsSlice'
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 EditVoice_sessions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    conversation: null,
    

    
    
    

    

    

    

    

    

    

    

    
    state: '',
    

    

    

    

    
    
    

    

    

    

    

    
    started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    ended_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'stt_provider': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tts_provider': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'detected_language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    audio_recordings: [],
    

    

    

    
    
    

    
    last_error: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { voice_sessionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Conversation' labelFor='conversation'>
        <Field
            name='conversation'
            id='conversation'
            component={SelectField}
            options={initialValues.conversation}
            itemRef={'conversations'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="State" labelFor="state">
        <Field name="state" id="state" component="select">
          
            <option value="idle">idle</option>
          
            <option value="listening">listening</option>
          
            <option value="processing">processing</option>
          
            <option value="speaking">speaking</option>
          
            <option value="ended">ended</option>
          
            <option value="error">error</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="STTProvider"
    >
        <Field
            name="stt_provider"
            placeholder="STTProvider"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TTSProvider"
    >
        <Field
            name="tts_provider"
            placeholder="TTSProvider"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DetectedLanguage"
    >
        <Field
            name="detected_language"
            placeholder="DetectedLanguage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  
    <FormField label="LastError" hasTextareaHeight>
        <Field name="last_error" as="textarea" placeholder="LastError" />
    </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('/voice_sessions/voice_sessions-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditVoice_sessions
