import { FC, ReactNode, useCallback } from "react" import { Pressable, Image, ImageStyle, Text, View, Alert, ImageSourcePropType, TextStyle } from "react-native" import React from "react" import { Skin } from "../core/skin" /* Importing the correct stylesheet */ import styles from "./style/Skin.style" import { useDispatch, useSelector } from "react-redux" import { useUserStore } from "../context/userContext" import { UserCoinsModifier } from "../core/User/userCoinsModifier" import UserSkinModifier from "../core/User/userSkinModifier" import { useStoreStore } from "../context/storeContext" import tabSkinApp from "../constSkin" import { MANAGER_USER } from "../../appManagers" export const SkinComponent: /* Parameters : * skin : Skin to be displayed * state : Indicates from wich screen the component has been called */ FC<{ nav: any, skin: Skin, state: String }> = ({ nav, skin, state }) => { const navigation = nav; const dispatch = useDispatch(); const setUser = useUserStore((state) => state.setUser); const setTabSkin = useStoreStore((state) => state.setTabSkin); async function changerSkin(skin: Skin) { const m = new UserSkinModifier(); const tmp = MANAGER_USER.getCurrentUser(); if (tmp !== null) { await m.changeCurrentSkin(tmp, skin); setUser(tmp); MANAGER_USER.setCurrentUser(tmp); } } const handleStoreChange = useCallback(async () => { let tabSkinStore = [...tabSkinApp]; // const tmp=MANAGER_USER.getCurrentUser()?.getTabSkin(); // if (tmp!=undefined){ MANAGER_USER.getCurrentUser()?.getTabSkin()?.forEach(skin => { for (let i = 0; i < tabSkinStore.length; i++) { if (skin.isEqual(tabSkinStore[i])) { tabSkinStore.splice(i, 1); } } }); setTabSkin(tabSkinStore); //} }, []); async function buySkin(skin: Skin) { const mSkin = new UserSkinModifier(); const mCoins = new UserCoinsModifier(); const tmp = MANAGER_USER.getCurrentUser(); if (tmp !== null) { await mCoins.removeCoins(tmp, skin.getSkinCost()).then(async (res) => { if (res == true) { await mSkin.addSkin(tmp, skin); setUser(tmp); MANAGER_USER.setCurrentUser(tmp); Alert.alert("Achat du skin"); handleStoreChange(); } else { Alert.alert("Pas assez d'argent pour acheter le skin"); } }); } } /* The display of this component depends of the screen from where it has been called: * From the TopBar (icon) : Small image in a circle * From the shop (shop) : Image + Name + Price, Pressable => Buy the skin * From the profile (profile) : Name + Image, Pressable => Change the skin */ switch (state) { case 'icon': return ( ) case 'shop': return ( buySkin(skin)} style={styles.imageWrapper}> {skin.getSkinName()} 100€ ) case 'liste': return ( { changerSkin(skin); navigation.goBack() }} style={styles.imageWrapper}> {skin.getSkinName()} ) case 'profile': return ( Alert.alert("cool")} style={styles.imageWrapperProfil}> {skin.getSkinName()} ) default: return ( ) } }