import React, { useState } from "react";
import { StyleSheet, View, TouchableOpacity } from "react-native";
import * as Yup from "yup";

import {
  Form,
  FormPicker as Picker,
  SubmitButton,
  FormField,
} from "../components/forms";
import Screen from "../components/Screen";
import FormImagePicker from "../components/forms/FormImagePicker";
import useLocation from "../hooks/useLocation";
import { Image, Text } from "react-native";
import colors from "../config/colors";
import { TouchableNativeFeedback } from "react-native-gesture-handler";
import { insertRiverData } from "../database/databaseLoader";
import { showMessage } from "react-native-flash-message";
import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view";
import assets from "../config/assets";
import { scaleDimsFromWidth, dimensions } from "../config/dimensions";
import moment from "moment";
import FormDatePicker from "../components/forms/FormDatePicker";
import FormLocationPicker from "../components/forms/FormLocationPicker";

const validationSchema = Yup.object().shape({
  images: Yup.array(),
  description: Yup.string().label("Description"),
});

const borderWidth = 4;

function RiverFloodSharingDataScreen(props) {
  const [riverScale, setRiverScale] = useState(-1);
  const location = useLocation();
  const [error, setError] = useState(false);

  const [date, setDate] = useState(moment());
  const [time, setTime] = useState(moment());

  const dims = scaleDimsFromWidth(85, 85, 20);
  return (
    <Screen style={styles.container}>
      <KeyboardAwareScrollView
        resetScrollToCoords={{ x: 0, y: 0 }}
        scrollEnabled={true}
      >
        <Form
          initialValues={{
            images: [],
            description: "",
          }}
          onSubmit={(values) => {
            if (riverScale == -1) {
              setError(true);
              return;
            }
            insertRiverData({ ...values, riverScale, location, date, time });
            showMessage({
              message: "Informação enviada!",
              duration: 1950,
              icon: "success",
              type: "success",
            });
            props.navigation.navigate("Home");
          }}
          validationSchema={validationSchema}
        >
          <View>
            <View style={styles.imgs_row}>
              <TouchableNativeFeedback onPress={() => setRiverScale(0)}>
                <View
                  borderColor={riverScale == 0 ? colors.primary : colors.white}
                  style={styles.img_block}
                >
                  <assets.riverLevel.Low {...dims} />
                </View>
              </TouchableNativeFeedback>

              <TouchableNativeFeedback onPress={() => setRiverScale(1)}>
                <View
                  borderColor={riverScale == 1 ? colors.primary : colors.white}
                  style={styles.img_block}
                >
                  <assets.riverLevel.Normal {...dims} />
                </View>
              </TouchableNativeFeedback>

              <TouchableNativeFeedback onPress={() => setRiverScale(2)}>
                <View
                  borderColor={riverScale == 2 ? colors.primary : colors.white}
                  style={styles.img_block}
                >
                  <assets.riverLevel.High {...dims} />
                </View>
              </TouchableNativeFeedback>

              <TouchableNativeFeedback onPress={() => setRiverScale(3)}>
                <View
                  borderColor={riverScale == 3 ? colors.primary : colors.white}
                  style={styles.img_block}
                >
                  <assets.riverLevel.Flooding {...dims} />
                </View>
              </TouchableNativeFeedback>
            </View>
          </View>

          {error && riverScale == -1 && (
            <Text style={styles.error_txt}>Selecione o nível do rio</Text>
          )}

          <FormImagePicker backgroundColor={colors.primary} name="images" />

          <View
            style={{
              flex: 1,
              flexDirection: "row",
              justifyContent: "space-between",
            }}
          >
            {/*Data da coleta:*/}
            <View
              style={{
                flex: 0.48,
                borderRightColor: colors.cerulean,
                borderRightWidth: 1,
              }}
            >
              <FormDatePicker
                textStyle={{
                  borderColor: colors.gray,
                  borderWidth: 3,
                }}
                defaultDate={new Date()}
                onDateChange={(value) => setDate(value)}
                onTimeChange={(value) => setTime(value)}
              />
            </View>
            {/*Local do evento:*/}
            <View style={{ flex: 0.48 }}>
              <TouchableOpacity
                onPress={() => props.navigation.navigate("FormMap")}
              >
                <FormLocationPicker />
              </TouchableOpacity>
            </View>
          </View>

          <FormField
            maxLength={255}
            multiline
            name="description"
            numberOfLines={3}
            placeholder="Descrição"
          />
          <SubmitButton title="Enviar" backgroundColor={colors.primary} />
        </Form>
      </KeyboardAwareScrollView>
    </Screen>
  );
}

const dims = scaleDimsFromWidth(85, 85, 4);

const styles = StyleSheet.create({
  container: {
    padding: 10,
    backgroundColor: colors.white,
  },
  img_block: {
    borderRadius: 5,
    borderStyle: "dotted",
    borderColor: colors.white,
    borderWidth: borderWidth,
    alignItems: "center",
  },
  imgs_row: {
    flexDirection: "row",
    justifyContent: "space-between",
  },
  text: {
    fontSize: dimensions.text.default,
    textAlign: "center",
    marginTop: 10,
  },
  error_txt: {
    fontSize: 18,
    color: colors.danger,
  },
});

export default RiverFloodSharingDataScreen;