import 'dart:convert';
import 'dart:developer';
import 'package:http/http.dart' as http;

import 'package:initial_folder/base_service.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:initial_folder/models/quiz_model.dart';
import 'package:initial_folder/models/quiz_perquestion_result_model.dart';
import 'package:initial_folder/models/quiz_question_model.dart';
import 'package:initial_folder/models/quiz_question_result_model.dart';

class quiz_service {
  Future get_quiz_info(String id) async {
    Uri url = Uri.parse('$baseUrl/users/quiz/question/$id');
    String? token = await UsersInfo().getToken();
    var response;
    if (token == null) {
      response = await http.get(url, headers: baseHeader);
    } else {
      response = await http.get(url, headers: headerWithToken(token));
    }
    log(response.body);
    if (response.statusCode == 200) {
      final data = quizModelFromJson(response.body);
      return data;
    } else {
      throw Exception('Data Detail Kursus Gagal Diambil');
    }
  }

  Future get_result_quiz(String resultArr) async {
    Uri url = Uri.parse('$baseUrl/users/quiz/question/check-answer');
    String? token = await UsersInfo().getToken();
    var response;
    // print(resultArr + "ini pas ngambil");
    if (token == null) {
      response = await http.get(url, headers: baseHeader);
    } else {
      print("disaodji" + resultArr);
      response = await http.post(url,
          headers: headerWithToken(token), body: resultArr);
    }
    if (response.statusCode == 200) {
      final data = quizQuestionResultFromJson(response.body);
      log(data.toString());
      return data;

      // return jsonData;
    } else {
      print("errrorr---------" + response.statusCode);
      throw Exception('Data Detail jawaban Gagal Diambil');
    }
  }

  Future get_result_quiz_pernumber(int quizId, int index) async {
    Uri url =
        Uri.parse('$baseUrl/users/quiz/question/${quizId}?slice=${index}');
    String? token = await UsersInfo().getToken();
    var response;
    // print(resultArr + "ini pas ngambil");
    if (token == null) {
      response = await http.get(url, headers: baseHeader);
    } else {
      // print("disaodji" + resultArr);
      response = await http.get(url, headers: headerWithToken(token));
    }
    if (response.statusCode == 200) {
      final data = quizPerQuestionResultFromJson(response.body);
      log(data.data.first.toString());
      return data;

      // return jsonData;
    } else {
      print("errrorr---------" + response.statusCode);
      throw Exception('Data Detail jawaban Gagal Diambil');
    }
  }
}
