import 'dart:math';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;


// const String baseUrl = "http://localhost:8123/smsApi/v1";
// const String baseUrl = "https://api.theonasanyas.com/smsApi/v1";
const String baseUrl = "http://10.15.45.29:8123/smsApi/v1";

const primaryColor = Color.fromARGB(255, 152, 120, 57);
const primaryLightColor = Color.fromARGB(150, 238, 224, 173);
const double defaultPadding = 15.0;
const timeout = 3;
var client = http.Client();

late String session;
late String semester;
late String loginId;
late Map<String, String> header;

setSharedPref(String key, var value) async {
  SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

  switch (value.runtimeType.toString()) {
    case 'String':
      sharedPreferences.setString(key, value);
      break;

    case 'double':
      sharedPreferences.setDouble(key, value);
      break;

    case 'int':
      sharedPreferences.setInt(key, value);
      break;

    case 'bool':
      sharedPreferences.setBool(key, value);
      break;
  }
}

getSharedPref(var key, var type) async {
  SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

  switch (type) {
    case 'String':
      return sharedPreferences.getString(key);

    case 'double':
      return sharedPreferences.getDouble(key);

    case 'int':
      return sharedPreferences.getInt(key);

    case 'bool':
      return sharedPreferences.getBool(key);
  }
}

Future<bool> checkTokenValidity() async {
  final value = await getSharedPref('tokenValidity', 'String');

  print("tokenValidity --- $value");
  DateTime dateTime;
  DateTime currentTime;
  if (value == '') {
    currentTime = DateTime.now();
    dateTime = DateTime.now();
  } else {
    currentTime = DateTime.now();
    dateTime = DateTime.parse(value);
  }

  if (dateTime.isAfter(currentTime)) {
    return true;
  } else {
    return false;
  }
}

String getImage() {
  //
  //

  List<String> image = [
    'assets/images/image1.jpg',
    'assets/images/image2.jpg',
    'assets/images/image3.jpg',
    'assets/images/image4.jpg',
    'assets/images/image5.jpg',
    'assets/images/image6.jpg',
    'assets/images/image7.jpg',
    'assets/images/image8.jpg'
  ];

  var rnd = Random();
  final index = rnd.nextInt(image.length);
  return image[index];
}

String studentGrade(double score) {
  //
  //
  String grade = "";
  switch (score) {
    case >= 70:
      grade = "Distinction";
      break;

    case >= 60:
      grade = "Merit";
      break;

    case >= 50:
      grade = "Pass";
      break;

    default:
      grade = "Fail";
      break;
  }

  return grade;
}
