/* eslint-disable prettier/prettier */
import React, { useState, useEffect } from "react";
import {
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  // TouchableNativeFeedback,
  Image,
  StatusBar,
  Button,
  Alert,
  Linking,
} from "react-native";
import { withNavigation } from "@react-navigation/compat";
import MCIcon from "react-native-vector-icons/MaterialCommunityIcons";
import Colors from "react-native-digitcamera/src/common/Colors";
// import CustomSwitch from "../common/Switch";
import {
  setI18nConfig,
  translate,
} from "react-native-digitcamera/src/common/localize/i18n";
import Database from "../../../../Components/common/DAsyncStorage";
import GetLocation from "react-native-digitcamera/src/common/LocationHandler";

const Header = ({
  homeScreen = true,
  leftActionPress,
  rightActionPress,
  searchBarVisible,
  rightAction,
  progress = 0.5,
  navigation,
  titleValue = null,
  goBackHome,
  hasTitle,
  customTitle,
  shiftTitleToLeft,
  type,
  hideTranslationToggle,
  logoPress = () => {},
  homeNavigation = false,
  currentStep,
  totalSteps,
  onbackPress,
  showSteps = false,
  onlanguagechange,
  hideArrowBackButton = false,
  onLogoutPress,
  isLogoutVisible,
  titleStyle,
  showOnlySteps,
  currentOnlyStep,
  totalOnlyStep,
}) => {
  const [index, setIndex] = useState("");
  var [selectedLang, SetSelectedLang] = useState("en");
  const [appTitle, setAppTitle] = useState("");
  // var RealmData = Database.get("SelectedLang");
  useEffect(() => {
    getHeaderTitle();
    console.log("hideArrowBackButton", hideArrowBackButton);
  }, []);

  const getHeaderTitle = async () => {
    const titleObj = await Database.get("PIAppTitle");
    console.log("($) Library Header App Title ", titleObj?.title);
    setAppTitle(titleObj?.title);
  };

  // const navigateToHome = () => {
  //   navigation.dispatch(
  //     CommonActions.reset({
  //       index: 1,
  //       routes: [
  //         {
  //           name: "BottomNavigation",
  //           state: {
  //             routes: [{ name: "Home" }],
  //           },
  //         },
  //       ],
  //     })
  //   );
  // };

  // logout
  const onLogout = () => {
    onLogoutPress();
  };

  //Go back
  const goback = () => {
    // setIndex(currentStep-1)
    onbackPress();
  };
  //call back func for changing lang on screen
  // const langChange = () => {
  //   onlanguagechange();
  // };
  //START

  //Change langaguge
  const onSelectSwitch = async (index) => {
    //lang selection
    if (index == 1) {
      selectedLang = "hn";
    } else {
      selectedLang = "en";
    }
    console.log("INDEX", index);
    await Database.update("SelectedLang", {
      PreferredLanguage: selectedLang,
    });
    // setI18nConfig();
    // langChange();
  };
  return (
    <View style={styles.main}>
      <View
        style={[
          styles.HeaderView,
          shiftTitleToLeft === true
            ? { justifyContent: "space-between" }
            : null,
        ]}
      >
        {/* Left Action */}
        {shiftTitleToLeft === true ? null : (
          <View style={styles.leftActionView}>
            {hideArrowBackButton ? null : (
              <TouchableOpacity
                onPress={() => {
                  goback();
                }}
              >
                <View
                  style={{ justifyContent: "center", alignItems: "center" }}
                >
                  <MCIcon
                    name="arrow-bottom-left"
                    size={24}
                    color="black"
                    style={{ transform: [{ rotate: "45deg" }], marginTop: 4 }}
                  />
                </View>
              </TouchableOpacity>
            )}
          </View>
        )}

        {/* App Icon */}
        <View style={styles.appNameView}>
          <View style={styles.appNamesubView}>
            <Text
              style={[
                styles.appNameText,
                shiftTitleToLeft ? { marginLeft: -8 } : null,
              ]}
            >
              {customTitle === undefined ? "" : customTitle}
            </Text>
            {showSteps ? (
              <Text style={styles.stepText}>
                {translate("STEP ")}
                {currentStep + 1 + "/" + totalSteps}
              </Text>
            ) : null}
            {showOnlySteps ? (
              <Text style={styles.stepText}>
                {`(${currentOnlyStep} / ${totalOnlyStep})`}
              </Text>
            ) : null}
          </View>
        </View>

        {/* Lang Toggle */}
        <View style={styles.langToggleView}>
          {/* <CustomSwitch
            roundCorner={true}
            option1={"हिन्दी"}
            option2={"English"}
            selectionColor={"#FFFFFF"}
            selectionTextColor={"#000000"}
            borderColor={"#DDDDDD"}
            unSelectionColor={"#F5F5F5"}
            unSelectionTextColor={"#999999"}
            onSelectSwitch={onSelectSwitch}
            selectionMode={RealmData[0]?.PreferredLanguage == "en" ? 2 : 1}
          ></CustomSwitch> */}
        </View>
        {isLogoutVisible && (
          <TouchableOpacity
            onPress={() => {
              onLogout();
            }}
          >
            <Text style={styles.logoutStyle}>{"Logout"}</Text>
          </TouchableOpacity>
        )}
      </View>
      <GetLocation />
    </View>
  );
};

export default withNavigation(Header);

const styles = StyleSheet.create({
  main: {
    backgroundColor: Colors.white,
  },
  HeaderView: {
    // height: 70,
    padding: 8,
    flexDirection: "row",
    alignItems: "center",
  },
  leftActionView: {
    flex: 0.1,
    justifyContent: "center",
  },
  appNameView: {
    flex: 0.6,
    justifyContent: "center",
  },
  langToggleView: {
    flex: 0.3,
    justifyContent: "center",
  },
  appNameText: {
    fontFamily: "Muli-Black",
    fontSize: 20,
  },
  appNamesubView: {
    fontFamily: "Muli-Black",
    fontSize: 20,
    marginLeft: 8,
    flexDirection: "row",
    alignItems: "center",
  },
  stepText: {
    fontFamily: "Muli-Bold",
    fontSize: 12,
    color: "#999999",
    marginLeft: 8,
  },
  logoutStyle: {
    fontFamily: "Muli-Bold",
    fontSize: 16,
    alignSelf: "center",
  },
});
