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/matches/matchesSlice'
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 EditMatches = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'game_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    host: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    mode: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    quiz: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_locked: false,
    

    

    

    

    

    

    
    
    

    

    

    
    max_players: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    question_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    randomize_questions: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    randomize_answers: false,
    

    

    

    

    

    

    
    
    

    

    

    
    wheel_event_every_n_questions: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    cat_interaction_every_n_questions: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    starting_speed_kmh: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    race_map: '',
    

    

    

    

    
    
    

    

    

    

    

    
    scheduled_start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    ended_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { matchesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="GameCode"
    >
        <Field
            name="game_code"
            placeholder="GameCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Mode' labelFor='mode'>
        <Field
            name='mode'
            id='mode'
            component={SelectField}
            options={initialValues.mode}
            itemRef={'game_modes'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Quiz' labelFor='quiz'>
        <Field
            name='quiz'
            id='quiz'
            component={SelectField}
            options={initialValues.quiz}
            itemRef={'quizzes'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="lobby">lobby</option>
          
            <option value="in_progress">in_progress</option>
          
            <option value="ended">ended</option>
          
            <option value="cancelled">cancelled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsLocked' labelFor='is_locked'>
        <Field
            name='is_locked'
            id='is_locked'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxPlayers"
    >
        <Field
            type="number"
            name="max_players"
            placeholder="MaxPlayers"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="QuestionCount"
    >
        <Field
            type="number"
            name="question_count"
            placeholder="QuestionCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='RandomizeQuestions' labelFor='randomize_questions'>
        <Field
            name='randomize_questions'
            id='randomize_questions'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='RandomizeAnswers' labelFor='randomize_answers'>
        <Field
            name='randomize_answers'
            id='randomize_answers'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="WheelEventEveryNQuestions"
    >
        <Field
            type="number"
            name="wheel_event_every_n_questions"
            placeholder="WheelEventEveryNQuestions"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CatInteractionEveryNQuestions"
    >
        <Field
            type="number"
            name="cat_interaction_every_n_questions"
            placeholder="CatInteractionEveryNQuestions"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="StartingSpeed(km/h)"
    >
        <Field
            type="number"
            name="starting_speed_kmh"
            placeholder="StartingSpeed(km/h)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RaceMap" labelFor="race_map">
        <Field name="race_map" id="race_map" component="select">
          
            <option value="san_francisco_run">san_francisco_run</option>
          
            <option value="milky_way_highway">milky_way_highway</option>
          
            <option value="ocean_drive">ocean_drive</option>
          
            <option value="theme_park_rush">theme_park_rush</option>
          
            <option value="actually_legal_highway_race">actually_legal_highway_race</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  


  

              <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('/matches/matches-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMatches
