import { Button, Text as NBText, Thumbnail, View } from "native-base";
import React from "react";
import { FlatList, SafeAreaView, Text } from "react-native";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import {
  removeAllPhotos,
  uploadAllPhotos,
} from "../../redux/photos/photos.actions";
import { selectPhotos } from "../../redux/photos/photos.selectors";

const mapStateToProps = createStructuredSelector({
  photos: selectPhotos,
});
const mapDispatchToProps = (dispatch) => ({
  removeAllPhotos: () => dispatch(removeAllPhotos()),
  uploadAllphotos: () => dispatch(uploadAllPhotos()),
});

export function ScreenMediaCache({ photos, removeAllPhotos, uploadAllphotos }) {
  return (
    <SafeAreaView style={{ display: "flex", flex: 1 }}>
      <Text>This is the media cache screen.</Text>
      <Button block onPress={() => removeAllPhotos()}>
        <NBText>Delete all</NBText>
      </Button>
      <Button block onPress={() => uploadAllphotos()}>
        <NBText>Upload all</NBText>
      </Button>
      <Text>{photos.length}</Text>

      <FlatList
        style={{ flex: 1 }}
        data={photos}
        keyExtractor={(item) => item.id}
        renderItem={(object) => (
          <View>
            <Text>{object.item.uri}</Text>
            {!object.item.video && (
              <Thumbnail square large source={{ uri: object.item.uri }} />
            )}
          </View>
        )}
      />
    </SafeAreaView>
  );
}
export default connect(mapStateToProps, mapDispatchToProps)(ScreenMediaCache);
