// noPage
import { LibToastProperty } from 'esoftplay/cache/lib/toast/import';
import esp from 'esoftplay/esp';

import * as FileSystem from 'expo-file-system';
import * as MediaLibrary from 'expo-media-library';
import { Platform } from 'react-native';
import { captureRef } from "react-native-view-shot";

export default class m {
  static capture(ref: any, name: string): void {
    MediaLibrary.requestPermissionsAsync().then((r) => {
      if (r.granted) {
        captureRef(ref, {
          format: "jpg",
          quality: 0.8
        }).then(
          (images: string) => {
            const uriArray = images.split("/");
            const nameToChange = uriArray[uriArray.length - 1];
            const renamedURI = images.replace(nameToChange, name.replace(/\s/g, '_') + ".jpg");


            if (Platform.OS == 'ios') {
              MediaLibrary.saveToLibraryAsync(images)
              LibToastProperty.show(esp.lang("lib/capture", "saved_to_gallery"))
              return
            }
            FileSystem.copyAsync({ from: images, to: renamedURI }).then(() => {
              MediaLibrary.saveToLibraryAsync(renamedURI)
              LibToastProperty.show(esp.lang("lib/capture", "saved_to_dcim"))
            }).catch((r) => {

            })
          },
          error => LibToastProperty.show(error)
        )
      }
    })
  }
}