import 'dart:io';

import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:hello_wallpaper/common/utils/local_path_util.dart';
import 'package:hello_wallpaper/generated/assets.dart';
import 'package:hello_wallpaper/res/values/strings.dart';
import 'package:hello_wallpaper/routes/app_pages.dart';
import 'package:share_plus/share_plus.dart';

class SettingsController extends GetxController {
  final privacyUrlAndroid = 'https://nowwallpaperapp.mystrikingly.com/privacy';
  final termsUrlAndroid = 'https://nowwallpaperapp.mystrikingly.com/terms';

  final privacyUrlIOS = 'https://nowwallpaper.mystrikingly.com/privacy';
  final termsUrlIOS = 'https://nowwallpaper.mystrikingly.com/terms';

  // final options = ['About', 'Feedback', 'Share', 'Privacy Policy', 'Terms of Service'];
  final options = ['About', 'Share', 'Privacy Policy', 'Terms of Service'];

  void onTapItem(int index) async {
    if (index == 0) {
      Get.toNamed(AppPages.about);
    }
    if (index == 1) {
      Share.shareXFiles(
        [await getImageFileFromAssets(Assets.iconIconApp)],
        text: appName,
      );
    }
    if (index == 2) {
      Get.toNamed(AppPages.privacyPolicy, arguments: {
        'title': options[index],
        'url': Platform.isIOS ? privacyUrlIOS : privacyUrlAndroid,
      });
    }
    if (index == 3) {
      Get.toNamed(AppPages.termsOfService, arguments: {
        'title': options[index],
        'url': Platform.isIOS ? termsUrlIOS : termsUrlAndroid,
      });
    }
  }

  Future<XFile> getImageFileFromAssets(String path) async {
    final byteData = await rootBundle.load(path);

    final file = File('${(await LocalPathUtil.getTemporaryPath()).path}/$path');
    await file.create(recursive: true);
    await file.writeAsBytes(byteData.buffer
        .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

    return XFile(file.path);
  }
}
