import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:process_run/shell_run.dart';

class ClashName {
  static String get platform {
    if (Platform.isWindows) return 'windows';
    if (Platform.isMacOS) return 'darwin';
    if (Platform.isLinux) return 'linux';
    return 'unknown';
  }

  static String get arch {
    return const String.fromEnvironment('OS_ARCH', defaultValue: 'arm64'); //amd64
  }

  static String get ext {
    if (Platform.isWindows) return '.exe';
    return '';
  }

  static String get name {
    return 'ccore-$platform-$arch$ext';
  }
}

class Paths {
  static Directory get assets {
    File mainFile = File(Platform.resolvedExecutable);
    String assetsPath = '../data/flutter_assets/assets';
    if (Platform.isMacOS) assetsPath = '../../Frameworks/App.framework/Resources/flutter_assets/assets';
    return Directory(path.normalize(path.join(mainFile.path, assetsPath)));
  }

  static Directory get assetsBin {
    return Directory(path.join(assets.path, 'bin'));
  }

  static Directory get assetsDep {
    return Directory(path.join(assets.path, 'dep'));
  }

  static Directory get config {
    return Directory(path.join(userHomePath, '.config', 'ccore'));
  }
}

class Files {
  static File get assetsClashCore {
    return File(path.join(Paths.assetsBin.path, ClashName.name));
  }

  static File get assetsClashService {
    return File(path.join(Paths.assetsBin.path, 'ccore-service-${ClashName.platform}-${ClashName.arch}${ClashName.ext}'));
  }
  static File get assetsCCore {
    return File(path.join(Paths.assetsBin.path, 'ccore-${ClashName.platform}-${ClashName.arch}${ClashName.ext}'));
  }

  static File get assetsSysProxyWin {
    return File(path.join(Paths.assetsBin.path, 'sysproxy64.exe'));
  }

  static File get assetsCountryMmdb {
    return File(path.join(Paths.assetsDep.path, 'Country.mmdb'));
  }
  static File get assetsGeosite {
    return File(path.join(Paths.assetsDep.path, 'geosite.dat'));
  }
  static File get assetsGeoIP {
    return File(path.join(Paths.assetsDep.path, 'geoip.dat'));
  }
  static File get assetsWintun {
    return File(path.join(Paths.assetsDep.path, 'wintun.dll'));
  }

  static File get assetsExample {
    return File(path.join(Paths.assetsDep.path, 'example.yaml'));
  }

  static File get configConfig {
    return File(path.join(Paths.config.path, '.config.json'));
  }

  static File get makeProxyConfig {
    return File(path.join(Paths.config.path, 'proxy.yaml'));
  }

  static File get makeInitProxyConfig {
    return File(path.join(Paths.config.path, 'init_proxy.yaml'));
  }

  static File get configCountryMmdb {
    return File(path.join(Paths.config.path, 'Country.mmdb'));
  }

  static File get configGeoIP {
    return File(path.join(Paths.config.path, 'geoip.dat'));
  }
  static File get configGeosite {
    return File(path.join(Paths.config.path, 'geosite.dat'));
  }

  static File get configWintun {
    return File(path.join(Paths.config.path, 'wintun.dll'));
  }

  static File get configExample {
    return File(path.join(Paths.config.path, 'example.yaml'));
  }
}