import AlertKonfirmasi from "@/components/alertKonfirmasi"
import ButtonBackHeader from "@/components/buttonBackHeader"
import { ButtonForm } from "@/components/buttonForm"
import { InputForm } from "@/components/inputForm"
import Styles from "@/constants/Styles"
import { Entypo } from "@expo/vector-icons"
import * as ImagePicker from 'expo-image-picker'
import { router, Stack } from "expo-router"
import { useState } from "react"
import { Image, Pressable, SafeAreaView, ScrollView, Text, ToastAndroid, View } from "react-native"

export default function EditBanner() {
   const [selectedImage, setSelectedImage] = useState<string | undefined>(undefined)

   const pickImageAsync = async () => {
      let result = await ImagePicker.launchImageLibraryAsync({
         mediaTypes: ['images'],
         allowsEditing: false,
         quality: 1,
      });

      if (!result.canceled) {
         setSelectedImage(result.assets[0].uri);
      } else {
         alert('Tidak ada gambar yang dipilih');
      }
   };

   return (
      <SafeAreaView>
         <Stack.Screen
            options={{
               headerLeft: () => <ButtonBackHeader onPress={() => { router.back() }} />,
               headerTitle: 'Edit Banner',
               headerTitleAlign: 'center',
            }}
         />
         <ScrollView>
            <View style={[Styles.p15, Styles.mb100]}>
               <View style={[Styles.mb15]}>
                  {
                     selectedImage != undefined ? (
                        <Pressable onPress={pickImageAsync}>
                           <Image src={selectedImage} style={{ resizeMode: 'contain', width: '100%', height: 100 }} />
                        </Pressable>
                     ) : (
                        <Pressable onPress={pickImageAsync} style={[Styles.wrapPaper, Styles.contentItemCenter]}>
                           <View style={{ justifyContent: 'center', alignItems: 'center' }}>
                              <Entypo name="image" size={50} color={'#aeaeae'} />
                              <Text style={[Styles.textInformation]}>Mohon unggah gambar dalam resolusi 1535 x 450 piksel untuk memastikan</Text>
                           </View>
                        </Pressable>
                     )
                  }
               </View>
               <InputForm label="Judul" type="numeric" placeholder="Judul" required />
               <ButtonForm
                  text="SIMPAN"
                  onPress={() => {
                     AlertKonfirmasi({
                        title: 'Konfirmasi',
                        desc: 'Apakah anda yakin ingin mengubah data?',
                        onPress: () => {
                           ToastAndroid.show('Berhasil mengubah data', ToastAndroid.SHORT)
                           router.push('/banner')
                        }
                     })
                  }} />
            </View>
         </ScrollView>
      </SafeAreaView>
   )
}