import { Control, Controller, FieldValues } from "react-hook-form";
import {
  Button,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from "react-native";
import { CustomTextInput } from "../../common/components/form/text_input";
import { LIGHT_THEME } from "../../common/const/theme";
import { LoginFormValues } from "../hooks/useLoggin";
import { OrDivision } from "../../common/components/form/or_division";
import { AntDesign } from "@expo/vector-icons";
import { Link, router } from "expo-router";
import { useTranslation } from "react-i18next";
import { LanguageIcon } from "../../lang/components/language_icon";

interface LoginFormProps {
  control: Control<LoginFormValues, any>;
  onSubmit: () => void;
}

export const LoginForm = ({ control, onSubmit }: LoginFormProps) => {
  const LANG = useTranslation();
  return (
    <ScrollView contentContainerStyle={styles.loginScroll}>
      <LanguageIcon />
      <Text style={styles.title}>{LANG.t("loginScreen.title")}</Text>
      <View style={{ height: 40 }} />
      <Controller
        name="email"
        control={control}
        defaultValue=""
        render={({
          field: { onChange, onBlur, value },
          formState: { errors },
        }) => (
          <CustomTextInput
            label={LANG.t("forms.email")}
            value={value}
            onBlur={onBlur}
            errors={errors.email?.message as string}
            textInputProps={{
              autoComplete: "email",
              autoCapitalize: "none",
              onChangeText: onChange,
            }}
          />
        )}
        rules={{
          required: "Email is required",
          pattern: { value: /\S+@\S+\.\S+/, message: "Invalid email" },
        }}
      />
      <Controller
        name="password"
        control={control}
        defaultValue=""
        render={({
          field: { onChange, onBlur, value },
          formState: { errors },
        }) => (
          <CustomTextInput
            label={LANG.t("forms.password")}
            isPassword
            value={value}
            onBlur={onBlur}
            errors={errors.password?.message as string}
            textInputProps={{
              autoComplete: "password",
              secureTextEntry: true,
              onChangeText: onChange,
            }}
          />
        )}
        rules={{ required: "Password is required" }}
      />
      <Link
        href={"/auth/reset_password"}
        replace
        style={{ width: "100%", textAlign: "right" }}
      >
        <Text style={{ textDecorationLine: "underline" }}>
          Recuperar contraseña
        </Text>
      </Link>
      <View style={{ height: "3%" }} />
      <TouchableOpacity onPress={onSubmit} style={styles.submitBtn}>
        <Text style={styles.submitBtnText}>
          {LANG.t("loginScreen.loginButton")}
        </Text>
      </TouchableOpacity>

      <OrDivision />

      <Button
        title={LANG.t("loginScreen.registerButton")}
        onPress={() => router.replace("/sign_up")}
      />
    </ScrollView>
  );
};

const styles = StyleSheet.create({
  mainContainer: {
    flex: 1,
    backgroundColor: LIGHT_THEME.color.background,
  },
  imageContainer: {
    height: "40%",
    overflow: "hidden",
    borderBottomLeftRadius: 70,
    elevation: 5,
  },
  loginContainer: {
    marginHorizontal: 20,
    marginTop: "-30%",
    marginBottom: 20,
    elevation: 5,
    borderRadius: 10,
    backgroundColor: LIGHT_THEME.color.white,
    height: "70%",
  },
  loginScroll: {
    padding: 20,
    gap: 10,
    justifyContent: "center",
  },
  title: { fontSize: 30, fontWeight: "bold", alignSelf: "center" },
  submitBtn: {
    backgroundColor: "black",
    borderRadius: 5,
    width: "100%",
    padding: 10,
    justifyContent: "center",
    alignItems: "center",
  },
  submitBtnText: {
    color: "white",
    fontSize: 20,
  },
});
