import 'dart:convert';
import 'dart:developer' as developer;

import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:skillcourt/entity/user_info.dart';

class HttpService {
  static const _endpoint = 'https://skillcourt.com';

  static Future<UserInfo> createUser(
      String email,
      String password,
      String fname,
      String lname,
      String birthdate,
      String phoneNumber,
      String userName) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final response = await http.post(
      Uri.parse('$_endpoint/create-profile'),
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8'
      },
      body: jsonEncode(<String, String>{
        'email': email,
        'password': password,
        'username': userName,
        'phone_number': phoneNumber,
        'date_of_birth': birthdate,
        'first_name': fname,
        'last_name': lname,
      }),
    );
    final Map<String, dynamic> jsonRes =
        jsonDecode(response.body) as Map<String, dynamic>;
    if (response.statusCode == 200) {
      // If the server did return a 200 CREATED response,
      // then parse the JSON.
      if (jsonRes.containsKey('session_id')) {
        prefs.setString('sessionId', jsonRes['session_id'].toString());
        developer.log('Status Code worked', error: response.body);
        return UserInfo.fromJson(jsonRes);
      } else {
        developer.log('Failed to create user.', error: response.body);
        throw Exception('Failed to create user.');
      }
    } else {
      // If the server did not return a 200 CREATED response,
      // then throw an exception.
      developer.log('Failed to load website.', error: response.body);
      throw Exception('Failed to load website.');
    }
  }

  static Future<bool> fetchUser(String email, String password) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final response = await http.post(
      Uri.parse('$_endpoint/sync-login'),
      headers: <String, String>{
        'Content-Type': 'application/json',
      },
      body: jsonEncode(<String, String>{
        'email': email,
        'password': password,
      }),
    );
    final Map<String, dynamic> jsonRes =
        jsonDecode(response.body) as Map<String, dynamic>;
    if (response.statusCode == 200) {
      // If the server did return a 200 OK response,
      // then parse the JSON.
      if (jsonRes.containsKey('session_id')) {
        developer.log('Logged in Player', error: response.body);
        prefs.setString('sessionId', jsonRes['session_id'].toString());
        return true;
      } else {
        developer.log('Failed to log in Player', error: response.body);
        return false;
      }
    } else {
      // If the server did not return a 200 OK response,
      // then throw an exception.
      developer.log('Failed to load website.', error: response.body);
      throw Exception('Failed to load user');
    }
  }
}
