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/game_rounds/game_roundsSlice'
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 EditGame_rounds = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    
    round_number: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    round_mode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    state: '',
    

    

    

    

    
    
    

    

    

    
    time_limit_ms: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    presented_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    answered_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    player_choice: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    correct_choice: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_correct: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    lose_reason: '',
    

    

    

    

    
    
    

    

    

    
    value_a: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    value_b: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    session: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    option_a_beatmap: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    option_b_beatmap: null,
    

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

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

  const { game_roundsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  
    <FormField
        label="RoundNumber"
    >
        <Field
            type="number"
            name="round_number"
            placeholder="RoundNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RoundMode" labelFor="round_mode">
        <Field name="round_mode" id="round_mode" component="select">
          
            <option value="beatmap">beatmap</option>
          
            <option value="mapper">mapper</option>
          
            <option value="artist">artist</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="State" labelFor="state">
        <Field name="state" id="state" component="select">
          
            <option value="pending">pending</option>
          
            <option value="answered">answered</option>
          
            <option value="timed_out">timed_out</option>
          
            <option value="resolved">resolved</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TimeLimitMs"
    >
        <Field
            type="number"
            name="time_limit_ms"
            placeholder="TimeLimitMs"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PlayerChoice" labelFor="player_choice">
        <Field name="player_choice" id="player_choice" component="select">
          
            <option value="a">a</option>
          
            <option value="b">b</option>
          
            <option value="none">none</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CorrectChoice" labelFor="correct_choice">
        <Field name="correct_choice" id="correct_choice" component="select">
          
            <option value="a">a</option>
          
            <option value="b">b</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsCorrect' labelFor='is_correct'>
        <Field
            name='is_correct'
            id='is_correct'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="LoseReason" labelFor="lose_reason">
        <Field name="lose_reason" id="lose_reason" component="select">
          
            <option value="wrong">wrong</option>
          
            <option value="timeout">timeout</option>
          
            <option value="none">none</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ValueA"
    >
        <Field
            type="number"
            name="value_a"
            placeholder="ValueA"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ValueB"
    >
        <Field
            type="number"
            name="value_b"
            placeholder="ValueB"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Session' labelFor='session'>
        <Field
            name='session'
            id='session'
            component={SelectField}
            options={initialValues.session}
            itemRef={'game_sessions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'session_token'}
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='OptionABeatmap' labelFor='option_a_beatmap'>
        <Field
            name='option_a_beatmap'
            id='option_a_beatmap'
            component={SelectField}
            options={initialValues.option_a_beatmap}
            itemRef={'beatmaps'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'difficulty_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditGame_rounds
