import { useState } from "react";
import { screenWidth } from "./metrics";
import { Share } from "react-native";


/*
    Executes before Render
*/
export const useConstructor = (callBack = () => { }) => {
    const [hasBeenCalled, setHasBeenCalled] = useState(false);
    if (hasBeenCalled) return;
    callBack();
    setHasBeenCalled(true);
}

const _MS_PER_SECOND = 1000;
const _MS_PER_MINUTE = _MS_PER_SECOND * 60;
const _MS_PER_HOUR = _MS_PER_MINUTE * 60;
const _MS_PER_DAY = _MS_PER_HOUR * 24;
const _MS_PER_MONTH = _MS_PER_DAY * 31;
const _MS_PER_YEAR = _MS_PER_MONTH * 12;

export const capitalize = (str) => str.charAt(0).toUpperCase() + str.substring(1, str.length).toLowerCase()

export const getDifferenceInDates = (startDate, endDate) => {
    var diff = Math.floor(startDate.getTime() - endDate.getTime());
    return {
        "year": Math.floor(diff / _MS_PER_YEAR),
        "month": Math.floor(diff / _MS_PER_MONTH),
        "day": Math.floor(diff / _MS_PER_DAY),
        "hour": Math.floor(diff / _MS_PER_HOUR),
        "minute": Math.floor(diff / _MS_PER_MINUTE),
        "second": Math.floor(diff / _MS_PER_SECOND),
    }

}

export const getTimestamp = (iso_date_str) => {
    let diffObj = getDifferenceInDates(new Date(), new Date(iso_date_str))
    var result = "Timestamp Not Found"
    for (const key of Object.keys(diffObj)) {
        if (diffObj[key] > 0) {
            result = `${diffObj[key]} ${capitalize(key)}${diffObj[key] > 1 ? "s" : ""} ago`
            break
        }
    }
    return result

}

const _getMonth = (index) => {
    const months = {
        0: "January",
        1: "Febraury",
        2: "March",
        3: "April",
        4: "May",
        5: "June",
        6: "July",
        7: "August",
        8: "September",
        9: "October",
        10: "November",
        11: "December"
    }
    if (Object.hasOwn(months, index)) {
        return months[index]

    } else {
        return "Month Not Found"
    }

}

export const getDate = (iso_date_str) => {
    let date = new Date(iso_date_str)
    return `${date.getDate()} ${_getMonth(date.getMonth())} ${date.getFullYear()}`
}

const MOBILE_SCREEN_BREAKPOINT = 480;
const TABLET_SCREEN_BREAKPOINT = 768;
const LARGE_SCREEN_BREAKPOINT = 1180;


export const MOBILE_SCREEN = "Mobile"
export const TABLET_SCREEN = "Tablet"
export const DESKTOP_SCREEN = "Desktop"

export const getScreenType = () => {
    let screenType;
    if (screenWidth <= MOBILE_SCREEN_BREAKPOINT) {
        screenType = MOBILE_SCREEN
    } else if (screenWidth <= TABLET_SCREEN_BREAKPOINT) {
        screenType = TABLET_SCREEN
    } else {
        screenType = DESKTOP_SCREEN
    }
    return screenType
}

export const validateEmail = (email) => {
    const emailRegex = "^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$"
    return String(email).match(emailRegex)
}


export const navigateToArticle = (navigation, article_id, article_slug) => {
    // navigation.navigate("HomePageNav",{screen:"MainNav",params:{screen:'NewsDetailPage',params:{slug: article_slug, id: article_id}}})

    navigation.push("NewsDetailPage", { slug: article_slug, id: article_id })
    navigation.navigate("NewsDetailPage",  { slug: article_slug, id: article_id })
}


export const navigateToListViewPage = (navigation, topic_type, topic) => {
    navigation.navigate("NewsListPage",  { type: topic_type, title: topic } )
}

export const navigateToHomePage = (navigation) => {
    navigation.navigate("MainPage")
}

export const onShare = async (myOptions) => {
    try {
      await Share.share(myOptions);
    } catch (error) {
      alert(error.message);
    }
  };