/* eslint-disable @typescript-eslint/no-unused-vars */
import {
  ActionIcon,
  ButtonCenteredOnly,
  CenterCustom,
  InformationBox,
  NewWrapper_V2,
  PhoneInputCustom,
  SelectCustom,
  Spacing,
  StackCustom,
  TextAreaCustom,
  TextCustom,
  TextInputCustom,
} from "@/components";
import { MainColor } from "@/constants/color-palet";
import { ICON_SIZE_XLARGE } from "@/constants/constans-value";
import DUMMY_IMAGE from "@/constants/dummy-image-value";
import { DEFAULT_COUNTRY, type CountryData } from "@/constants/countries";
import Portofolio_ButtonCreate from "@/screens/Portofolio/ButtonCreatePortofolio";
import {
  apiMasterBidangBisnis,
  apiMasterSubBidangBisnis,
} from "@/service/api-client/api-master";
import {
  IMasterBidangBisnis,
  IMasterSubBidangBisnis,
} from "@/types/Type-Master";
import pickImage from "@/utils/pickImage";
import { Ionicons } from "@expo/vector-icons";
import { useFocusEffect, useLocalSearchParams } from "expo-router";
import _ from "lodash";
import { useCallback, useState } from "react";
import { Text, View } from "react-native";
import { Avatar } from "react-native-paper";

export function ScreenPortofolioCreate() {
  const { id } = useLocalSearchParams();
  const [selectedCountry, setSelectedCountry] = useState<CountryData>(DEFAULT_COUNTRY);
  const [phoneNumber, setPhoneNumber] = useState<string>("");
  const [data, setData] = useState({
    namaBisnis: "",
    masterBidangBisnisId: "",
    alamatKantor: "",
    tlpn: "",
    deskripsi: "",
  });
  const [imageUri, setImageUri] = useState<string | null>(null);

  const [bidangBisnis, setBidangBisnis] = useState<IMasterBidangBisnis[]>([]);
  const [subBidangBisnis, setSubBidangBisnis] = useState<
    IMasterSubBidangBisnis[]
  >([]);

  const [selectedSubBidang, setSelectedSubBidang] = useState<string[]>([]);
  const [listSubBidangSelected, setListSubBidangSelected] = useState([
    {
      id: "",
    },
  ]);

  const [dataMedsos, setDataMedsos] = useState({
    facebook: "",
    twitter: "",
    instagram: "",
    youtube: "",
    tiktok: "",
  });

  const [isLoadingCreate, setIsLoadingCreate] = useState(false);

  useFocusEffect(
    useCallback(() => {
      onLoadMaster();
      onLoadMasterSubBidangBisnis();
    }, []),
  );

  const onLoadMaster = async () => {
    try {
      const response = await apiMasterBidangBisnis();
      setBidangBisnis(response.data);
    } catch (error) {
      setBidangBisnis([]);
      console.log("Error onLoadMasterBidangBisnis", error);
    }
  };

  const onLoadMasterSubBidangBisnis = async () => {
    try {
      const response = await apiMasterSubBidangBisnis({});
      setSubBidangBisnis(response.data);
    } catch (error) {
      setSubBidangBisnis([]);
      console.log("Error onLoadMasterSubBidangBisnis", error);
    }
  };

  const handlerSelectedSubBidang = ({ id }: { id: string }) => {
    const selectedList = subBidangBisnis?.filter(
      (item) => (item?.masterBidangBisnisId as any) === id,
    );
    setSelectedSubBidang(selectedList as any[]);
  };

  const handlePhoneChange = (phone: string) => {
    setPhoneNumber(phone);
    
    // Format phone number for API
    const callingCode = selectedCountry.callingCode;
    let fixNumber = phone.replace(/\s+/g, "").replace(/^0+/, "");
    
    // Remove country code if already present
    if (fixNumber.startsWith(callingCode)) {
      fixNumber = fixNumber.substring(callingCode.length);
    }
    
    // Remove leading zero
    fixNumber = fixNumber.replace(/^0+/, "");
    
    const realNumber = callingCode + fixNumber;
    setData({ ...data, tlpn: realNumber });
  };

  const handleCountryChange = (country: CountryData) => {
    setSelectedCountry(country);
    
    // Re-format with new country code
    const callingCode = country.callingCode;
    let fixNumber = phoneNumber.replace(/\s+/g, "").replace(/^0+/, "");
    
    // Remove country code if already present
    if (fixNumber.startsWith(callingCode)) {
      fixNumber = fixNumber.substring(callingCode.length);
    }
    
    // Remove leading zero
    fixNumber = fixNumber.replace(/^0+/, "");
    
    const realNumber = callingCode + fixNumber;
    setData({ ...data, tlpn: realNumber });
  };

  return (
    <NewWrapper_V2
      enableKeyboardHandling
      keyboardScrollOffset={100}
      footerComponent={
        <Portofolio_ButtonCreate
          id={id as string}
          data={data}
          dataMedsos={dataMedsos}
          imageUri={imageUri}
          subBidangSelected={listSubBidangSelected}
          isLoadingCreate={isLoadingCreate}
          setIsLoadingCreate={setIsLoadingCreate}
        />
      }
    >
      <StackCustom gap="xs">
        <InformationBox text="Lengkapi data bisnis anda." />

        <View onStartShouldSetResponder={() => true}>
          <TextInputCustom
            required
            label="Nama Bisnis"
            placeholder="Masukkan nama bisnis"
            onChangeText={(value: any) => setData({ ...data, namaBisnis: value })}
          />
        </View>

        <View onStartShouldSetResponder={() => true}>
          <SelectCustom
            label="Bidang Usaha"
            required
            data={bidangBisnis.map((item) => ({
              label: item.name,
              value: item.id,
            }))}
            value={data.masterBidangBisnisId}
            onChange={(value) => {
              const isSameBidang = data.masterBidangBisnisId === value;

              if (!isSameBidang) {
                setListSubBidangSelected([{ id: "" }]);
              }

              setData({ ...(data as any), masterBidangBisnisId: value });
              handlerSelectedSubBidang({ id: value as string });
            }}
          />
        </View>

        {listSubBidangSelected.map((item, index) => (
          <View onStartShouldSetResponder={() => true} key={index}>
            <SelectCustom
              disabled={data.masterBidangBisnisId === ""}
              label="Sub Bidang Usaha"
              required
              data={_.map(selectedSubBidang as any)
                .filter((option: any) => {
                  const selectedValues = listSubBidangSelected.map((s) => s.id);
                  return (
                    option.id === item.id || !selectedValues.includes(option.id)
                  );
                })
                .map((e: any) => ({
                  value: e.id,
                  label: e.name,
                }))}
              value={item.id || null}
              onChange={(value) => {
                const list = _.clone(listSubBidangSelected);
                list[index].id = value as any;
                setListSubBidangSelected(list);
              }}
            />
          </View>
        ))}

        <CenterCustom>
          <View style={{ flexDirection: "row", alignItems: "center", gap: 10 }}>
            <ActionIcon
              disabled={
                selectedSubBidang.length === listSubBidangSelected.length
              }
              onPress={() => {
                setListSubBidangSelected([
                  ...listSubBidangSelected,
                  { id: "" },
                ]);
              }}
              icon={
                <Ionicons
                  name="add-circle-outline"
                  size={ICON_SIZE_XLARGE}
                  color={MainColor.black}
                />
              }
              size="xl"
            />
            <ActionIcon
              disabled={listSubBidangSelected.length <= 1}
              onPress={() => {
                const list = _.clone(listSubBidangSelected);
                list.pop();
                setListSubBidangSelected(list);
              }}
              icon={
                <Ionicons
                  name="remove-circle-outline"
                  size={ICON_SIZE_XLARGE}
                  color={MainColor.black}
                />
              }
              size="xl"
            />
          </View>
        </CenterCustom>

        <Spacing />

        <View>
          <View style={{ flexDirection: "row", alignItems: "center" }}>
            <TextCustom semiBold style={{ color: MainColor.white_gray }}>
              Nomor Telepon
            </TextCustom>
            <Text style={{ color: "red" }}> *</Text>
          </View>
          <Spacing height={5} />
          <PhoneInputCustom
            value={phoneNumber}
            onChangePhoneNumber={handlePhoneChange}
            selectedCountry={selectedCountry}
            onChangeCountry={handleCountryChange}
            placeholder="xxx-xxx-xxx"
          />
        </View>

        <Spacing />

        <View onStartShouldSetResponder={() => true}>
          <TextInputCustom
            required
            label="Alamat Bisnis"
            placeholder="Masukkan alamat bisnis"
            onChangeText={(value: any) =>
              setData({ ...data, alamatKantor: value })
            }
          />
        </View>

        <View onStartShouldSetResponder={() => true}>
          <TextAreaCustom
            label="Deskripsi Bisnis"
            placeholder="Masukkan deskripsi bisnis"
            value={data.deskripsi}
            onChangeText={(value: any) => setData({ ...data, deskripsi: value })}
            autosize
            minRows={2}
            maxRows={5}
            required
            showCount
            maxLength={1000}
          />
        </View>

        <Spacing />

        <InformationBox text="Upload logo bisnis anda untuk di tampilaka pada portofolio." />

        <CenterCustom>
          <Avatar.Image
            source={imageUri ? { uri: imageUri } : DUMMY_IMAGE.dummy_image}
            size={200}
          />
        </CenterCustom>

        <Spacing />

        <ButtonCenteredOnly
          icon="upload"
          onPress={() => {
            pickImage({
              setImageUri,
            });
          }}
        >
          Upload
        </ButtonCenteredOnly>

        <Spacing height={40} />

        <InformationBox text="Isi hanya pada sosial media yang anda miliki." />

        <View onStartShouldSetResponder={() => true}>
          <TextInputCustom
            label="Tiktok"
            placeholder="Masukkan username tiktok"
            onChangeText={(value: any) =>
              setDataMedsos({ ...dataMedsos, tiktok: value })
            }
          />
        </View>

        <View onStartShouldSetResponder={() => true}>
          <TextInputCustom
            label="Facebook"
            placeholder="Masukkan username facebook"
            onChangeText={(value: any) =>
              setDataMedsos({ ...dataMedsos, facebook: value })
            }
          />
        </View>

        <View onStartShouldSetResponder={() => true}>
          <TextInputCustom
            label="Instagram"
            placeholder="Masukkan username instagram"
            onChangeText={(value: any) =>
              setDataMedsos({ ...dataMedsos, instagram: value })
            }
          />
        </View>

        <View onStartShouldSetResponder={() => true}>
          <TextInputCustom
            label="Twitter"
            placeholder="Masukkan username twitter"
            onChangeText={(value: any) =>
              setDataMedsos({ ...dataMedsos, twitter: value })
            }
          />
        </View>

        <View onStartShouldSetResponder={() => true}>
          <TextInputCustom
            label="Youtube"
            placeholder="Masukkan username youtube"
            onChangeText={(value: any) =>
              setDataMedsos({ ...dataMedsos, youtube: value })
            }
          />
        </View>
      </StackCustom>
    </NewWrapper_V2>
  );
}
