import React, {useContext, useEffect, useRef, useState} from 'react';
import {ScrollView, Text, useColorScheme} from 'react-native';
import {Button, TextInput} from 'react-native-paper';
import {SnackbarContext} from './App';
import MassiveInput from './MassiveInput';
import {DatabaseContext} from './Routes';
import Set from './set';

export default function SetForm({
  save,
  set,
  workouts,
}: {
  set: Set;
  save: (set: Set) => void;
  workouts?: string[];
}) {
  const [name, setName] = useState(set.name);
  const [reps, setReps] = useState(set.reps.toString());
  const [weight, setWeight] = useState(set.weight.toString());
  const [unit, setUnit] = useState(set.unit);
  const [uri, setUri] = useState(set.image);
  const [created, setCreated] = useState(set.created);
  const [selection, setSelection] = useState({
    start: 0,
    end: set.reps.toString().length,
  });
  const weightRef = useRef<any>(null);
  const repsRef = useRef<any>(null);
  const dark = useColorScheme() === 'dark';
  const db = useContext(DatabaseContext);
  const {toast} = useContext(SnackbarContext);

  useEffect(() => {
    console.log('SetForm.useEffect:', {uri, name: set.name});
    if (!uri)
      db.executeSql(`SELECT image FROM sets WHERE name = ? LIMIT 1`, [
        set.name,
      ]).then(([result]) => setUri(result.rows.item(0)?.image));
  }, [uri, db, set.name]);

  const handleSubmit = () => {
    if (!name) return;
    if (created && !created.match(/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d$/))
      return toast(
        'Created must be of the format YYYY-mm-ddTHH:mm:ss. E.g. 1996-12-24T12:59:40',
        7000,
      );
    save({
      name,
      reps: Number(reps),
      created,
      weight: Number(weight),
      id: set.id,
      unit,
      image: uri,
    });
  };

  return (
    <>
      <ScrollView style={{height: '90%'}}>
        <MassiveInput
          label="Name"
          value={name}
          onChangeText={setName}
          autoCorrect={false}
          autoFocus={!name}
          onSubmitEditing={() => repsRef.current?.focus()}
        />
        <MassiveInput
          label="Reps"
          keyboardType="numeric"
          value={reps}
          onChangeText={setReps}
          onSubmitEditing={() => weightRef.current?.focus()}
          selection={selection}
          onSelectionChange={e => setSelection(e.nativeEvent.selection)}
          autoFocus={!!name}
          blurOnSubmit={false}
          innerRef={repsRef}
        />
        <MassiveInput
          label="Weight"
          keyboardType="numeric"
          value={weight}
          onChangeText={setWeight}
          onSubmitEditing={handleSubmit}
          innerRef={weightRef}
        />
        <MassiveInput
          label="Unit (kg)"
          value={unit}
          onChangeText={setUnit}
          onSubmitEditing={handleSubmit}
        />
        {set.created && (
          <TextInput
            label="Created"
            value={created}
            onChangeText={setCreated}
            selectionColor={dark ? '#2A2A2A' : ''}
            mode="outlined"
            style={{marginBottom: 10}}
          />
        )}
        <Text style={{marginBottom: 10}}>
          {workouts?.map((workout, index) => (
            <React.Fragment key={workout}>
              <Text
                style={{
                  fontWeight: workout === name ? 'bold' : 'normal',
                  textDecorationLine: workout === name ? 'underline' : 'none',
                }}>
                {workout}
              </Text>
              {index === workouts.length - 1 ? '.' : ', '}
            </React.Fragment>
          ))}
        </Text>
      </ScrollView>
      <Button
        disabled={!name}
        mode="contained"
        icon="save"
        onPress={handleSubmit}>
        Save
      </Button>
    </>
  );
}
