import React, { useState } from "react";
import { Dialog, TextInput, useTheme, Button, Text } from "react-native-paper";
import { View } from "react-native";
import axios from "axios";
import * as Location from "expo-location";
import log from "@/util/log"

export const API_URL = process.env.EXPO_PUBLIC_API_URL;

const LocationComponent = () => {
    const theme = useTheme();
    const [zip, setZip] = useState("");
    const [loading, setLoading] = useState(false);
    const [locLoading, setLocLoading] = useState(false);

    const handleSubmit = async () => {
        if (!zip) return;
        setLoading(true);
        try {
            const response = await axios.post(API_URL + "/zipLookup", { zip });
            log.error(response.data);
            const long = response.data[0];
            const lat = response.data[1];
        } catch (err) {
            log.error(err);
        }
        setLoading(false);
    };

    const handleGetLocation = async () => {
        setLocLoading(true);
        try {
            let { status } = await Location.requestForegroundPermissionsAsync();
            if (status !== 'granted') {
                log.error("Permission to access location was denied");
                setLocLoading(false);
                return;
            }
            let location = await Location.getCurrentPositionAsync({});
            const { longitude, latitude } = location.coords;
            log.error([longitude, latitude]);
        } catch (err) {
            log.error("Location Error:", err);
        }
        setLocLoading(false);
    };

    return (
        <Dialog.Content style={{ maxHeight: 360, justifyContent: "center", alignItems: "center" }}>
            <View style={{ alignItems: "center", width: "100%" }}>
                <Button
                    mode="outlined"
                    onPress={handleGetLocation}
                    loading={locLoading}
                    disabled={locLoading}
                    style={{ marginBottom: 12, width: 200 }}
                >
                    Use My Location
                </Button>
                <Text style={{ marginBottom: 12, color: theme.colors.primary, fontWeight: "bold" }}>OR</Text>
                <View style={{ width: 240, alignItems: "center" }}>
                    <TextInput
                        label="Enter Zip Code"
                        mode="outlined"
                        value={zip}
                        onChangeText={setZip}
                        style={{ marginBottom: 10, fontFamily: "SpaceReg", width: "100%" }}
                        placeholderTextColor={theme.colors.primary}
                        textColor={theme.colors.primary}
                        theme={{ colors: { text: theme.colors.primary } }}
                    />
                    <Button
                        mode="contained"
                        onPress={handleSubmit}
                        loading={loading}
                        disabled={!zip || loading}
                        style={{ width: "100%" }}
                    >
                        Submit
                    </Button>
                </View>
            </View>
        </Dialog.Content>
    );
};

export default LocationComponent;
