import { Alert, Platform } from 'react-native';
import RNCreedonImagepicker from 'react-native-creedon-imagepicker';
import OpenAppSettings from 'react-native-app-settings';
import i18n from '~/locale/i18n';
import { config } from '~/config';
import _ from 'lodash';

const defaultPickerOptions = {
	title: i18n.t('albums.albums'),
	mediaType: 'mixed',
	multiple: true,
	videoSelectionLimit: i18n.t('contents.videoSelectionLimit', { count: config.defaultMaxVideo }),
	imageSelectionLimit: i18n.t('contents.imageSelectionLimit', { count: config.defaultMaxImage }),
	okButtonTitle: i18n.t('common.ok'),
	quality: 100,
	noData: true,
	allowsMultipleSelection: '1',
};

const defaultPermissionDenied = {
	title: i18n.t('contents.shareOnNixplay'),
	text: i18n.t('contents.allowNixplayToAccessYourCameraRoll'),
	reTryTitle: i18n.t('contents.enableLibraryAccess'),
	okTitle: i18n.t('common.ok'),
};

export default () => ({
	async getAssets(options, isConnected) {
		try {
			console.log('SDK>getAssets: ', isConnected);
			// if (!isConnected) {
			// 	throw Error(i18n.t('common.noInternetConnection'));
			// }
			const pickerOptions = (options === undefined || options === null) ?
				defaultPickerOptions : options;
			const permissionDenied = defaultPermissionDenied;
			const response = await RNCreedonImagepicker.launchImageLibrary({ pickerOptions, permissionDenied });
			return response;
		} catch (error) {
			if (error.message.includes(RNCreedonImagepicker.PHOTO_LIBRARY_PERMISSIONS_NOT_GRANTED)) {
				if (Platform.OS === 'ios') {
					Alert.alert(
						i18n.t('contents.shareOnNixplay'),
						i18n.t('contents.allowNixplayToAccessYourCameraRoll'),
						[
							{ text: i18n.t('common.cancel'), onPress: () => {}, style: 'cancel' },
							{
								text: i18n.t('contents.enableLibraryAccess'),
								onPress: () => {
									OpenAppSettings.open();
								},
							},
						],
						{ cancelable: false },
					);
				}
				throw new Error('cancel');
			}
			throw new Error(error.message);
		}
	},
	async exportAssets(items, optimizeUpload, isConnected) {
		try {
			if (!isConnected) {
				throw Error(i18n.t('common.noInternetConnection'));
			}
			const defaultExportOption = {
				assets: _.map(items, (item) => item.uri.replace('ph://', '')),
				itemOptions: _.map(items, (item) => ({
					...item,
					caption: (item.caption || ''),
				})),
				fileSizeLimit: config.fileSizeLimit,
				widthLimit: config.widthLimit,
				heightLimit: config.widthLimit,
				quality: 100,
			};
			const exportOptions = optimizeUpload ? {
				...defaultExportOption,
				maxWidth: config.maxWidthOptimize,
				maxHeight: config.maxHeightOptimize,
				quality: 90,
			} : defaultExportOption;
			const response = await RNCreedonImagepicker.exportAssets({ exportOptions });
			return response;
		} catch (error) {
			throw new Error(error.message);
		}
	},
	async cancelExporting() {
		try {
			return await RNCreedonImagepicker.cancel({});
		} catch (error) {
			throw new Error(error.message);
		}
	},

});
