import {
  BaseBox,
  BoxButtonOnFooter,
  ButtonCustom,
  ViewWrapper,
} from "@/components";
import { RadioCustom, RadioGroup } from "@/components/Radio/RadioCustom";
import { LOCAL_STORAGE_KEY } from "@/constants/local-storage-key";
import { useAuth } from "@/hooks/use-auth";
import { dummyMasterBank } from "@/lib/dummy-data/_master/bank";
import { apiDonationCreateInvoice } from "@/service/api-client/api-donation";
import { apiMasterBank } from "@/service/api-client/api-master";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { router, useFocusEffect, useLocalSearchParams } from "expo-router";
import _ from "lodash";
import { useCallback, useEffect, useState } from "react";

export default function DonationSelectBank() {
  const { user } = useAuth();
  const { id } = useLocalSearchParams();

  console.log("id", id);
  const [select, setSelect] = useState<any | number>("");
  const [listBank, setListBank] = useState<any>([]);
  const [isLoading, setIsLoading] = useState<boolean>(false);

  useEffect(() => {
    loadListBank();
  }, []);

  const loadListBank = async () => {
    try {
      const response = await apiMasterBank();

      setListBank(response.data);
    } catch (error) {
      console.log("[ERROR]", error);
      setListBank([]);
    }
  };

  const handlerSubmit = async () => {
    try {
      setIsLoading(true);
      const dataStorage = await AsyncStorage.getItem(
        LOCAL_STORAGE_KEY.transactionDonation
      );

      if (dataStorage) {
        const storage = JSON.parse(dataStorage);
        const newData = {
          ...storage,
          bankId: select,
          authorId: user?.id,
        };

        console.log("[NEW DATA]", newData);

        const response = await apiDonationCreateInvoice({
          id: id as string,
          data: newData,
        });
        console.log("[RESPONSE CREATE>>]", response);

        if (response.success) {
          const invoiceId = response.data.id;

          const delStorage = await AsyncStorage.removeItem(
            LOCAL_STORAGE_KEY.transactionDonation
          );

          console.log("[DEL STORAGE]", delStorage);
          router.replace(
            `/(application)/(user)/donation/[id]/(transaction-flow)/${invoiceId}/invoice`
          );
        } else {
          console.log("[FAILED]", response);
        }
      }
    } catch (error) {
      console.log("[ERROR]", error);
    } finally {
      setIsLoading(false);
    }
  };

  const buttonSubmit = () => {
    return (
      <>
        <BoxButtonOnFooter>
          <ButtonCustom
            isLoading={isLoading}
            disabled={!select}
            onPress={() => handlerSubmit()}
          >
            Pilih
          </ButtonCustom>
        </BoxButtonOnFooter>
      </>
    );
  };
  return (
    <ViewWrapper footerComponent={buttonSubmit()}>
      <RadioGroup value={select} onChange={setSelect}>
        {_.isEmpty(listBank)
          ? []
          : listBank?.map((item: any, index: number) => (
              <BaseBox key={index}>
                <RadioCustom label={item.namaBank} value={item.id} />
              </BaseBox>
            ))}
      </RadioGroup>
    </ViewWrapper>
  );
}
