import 'package:betting/commom/app_cache.dart';
import 'package:betting/commom/utils/app_utils.dart';
import 'package:betting/data/repository/profile/profile_repo.dart';
import 'package:device_info/device_info.dart';
import 'package:get/get.dart';
import 'package:package_info/package_info.dart';
import 'dart:io';
import '../../../../data/base_service/api_response.dart';
import '../../../../data/repository/auth_login/auth_login_repo.dart';
import '../../../../data/repository/user/user_repo.dart';
import '../../../../routes/app_pages.dart';

class StartedController extends GetxController {
  final AuthRepo authRepo = AuthRepo();
  final ProfileRepo profileRepo = ProfileRepo();
  final UserRepo userRepo = UserRepo();
  RxBool isReady = false.obs;

  @override
  void onInit() async {
    if( AppCache().isFirstOpenApp){
      isReady.value = true;
      deleteUser();
      AppCache().deleteInfoLogin();
    }
    String? userName = await AppCache().username ??"";
    String? password = await AppCache().password ??"";
    if(userName !=  "" && password != ""){
      requestToken();
    }else{
      isReady.value = true;
    }

    AppCache().setIsFirstOpenApp(false);
    super.onInit();
  }



  requestToken() async{
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String? deviceId = "";
    String? appBundleID = packageInfo.packageName;
    String? devicePlatform = "";
    String? appName = packageInfo.appName;
    String? deviceAppVersion = packageInfo.version;
    String? deviceAppBuildNumber = packageInfo.buildNumber;


    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceId = androidInfo.id;
      devicePlatform = "android";
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      deviceId = iosInfo.identifierForVendor;
      devicePlatform = "ios";
    }

    final fcmKey = AppCache().fcmToken ?? "";
    await authRepo.requestToken(deviceId,appBundleID, fcmKey).then((value) async{
      if (value.state == Status.SUCCESS) {
        var userType = value.object?.userType ?? "";
        AppCache().setUserType(userType);
        AppCache().saveLogin(deviceId!, appBundleID);
        await getProfile();
      } else {
        AppUtils.shared.hideLoading();
      }
    });

  }


  deleteUser()async{
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String? deviceId = "";
    String? appBundleID = packageInfo.packageName;
    String? devicePlatform = "";
    String? appName = packageInfo.appName;
    String? deviceAppVersion = packageInfo.version;
    String? deviceAppBuildNumber = packageInfo.buildNumber;


    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceId = androidInfo.id;
      devicePlatform = "android";
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      deviceId = iosInfo.identifierForVendor;
      devicePlatform = "ios";
    }
    await userRepo.deleteUser(deviceId).then((value) {
      if (value.state == Status.SUCCESS) {


      }
    });
  }


  getProfile() async{
    await profileRepo.getProfile().then((value) {
      if (value.state == Status.SUCCESS) {
        var isSkip = value.object!.isSkip!;
        if(isSkip){
          Get.toNamed(Routes.home);
        }
        else{
          Get.toNamed(Routes.followTeamsPage);
        }

      }
    });
  }


}



