import 'dart:convert';
import 'package:initial_folder/base_service.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:initial_folder/models/detail_course_model.dart';
import 'package:initial_folder/models/detail_rating_course_model.dart';
import 'package:initial_folder/models/course_model.dart';
import 'package:initial_folder/models/my_course_model.dart';
import 'package:http/http.dart' as http;

class CourseService {
  Future<List<CourseModel>> getOthersCourse(page) async {
    Uri url = Uri.parse('$baseUrl/homepage/courses?page=$page&limit=10');

    http.Response response = await http.get(url, headers: baseHeader);
    if (response.statusCode == 200) {
      var data = jsonDecode(response.body)['data'][0];
      List<CourseModel> course = [];
      for (var item in data) {
        course.add(CourseModel.fromJson(item));
      }

      return course;
    } else {
      throw Exception('Data Kursus lainnya Gagal Diambil');
    }
  }

  Future<DetailCourseModel> getDetailCourse(String id) async {
    Uri url = Uri.parse('$baseUrl/homepage/course/$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));
    }
    if (response.statusCode == 200) {
      print("Berhasil detail course ${response.body}");
      return DetailCourseModel.fromJson(jsonDecode(response.body));
    } else {
      print("Gagal detail course ${response.body}");
      throw Exception('Data Detail Kursus Gagal Diambil');
    }
  }

  Future<DataDetailCourseModel> getDetailCourseCoupon(
      String id, String coupon) async {
    Uri url = Uri.parse('$baseUrl/mobile/course-discount/$id/$coupon');
    http.Response response = await http.get(url, headers: baseHeader);
    if (response.statusCode == 200) {
      Map body = jsonDecode(response.body);
      var data = body['data'];
      print("Ini sukses detail kupon ${response.body}");
      return DataDetailCourseModel.fromJson(data);
    } else {
      print("Ini gagal detail kupon ${response.body}");
      throw Exception('Data Detail Kursus Gagal Diambil');
    }
  }

  Future<DetailCourseModel> getDetailCourseLogin(String id) async {
    String? token = await UsersInfo().getToken();

    Uri url = Uri.parse('$baseUrl/homepage/course/$id');
    http.Response response =
        await http.get(url, headers: headerWithToken(token));

    if (response.statusCode == 200) {
      print("Berhasil detail course ${response.body}");
      return DetailCourseModel.fromJson(jsonDecode(response.body));
    } else {
      print("Gagal detail course ${response.body}");
      throw Exception('Data Detail Kursus Gagal Diambil');
    }
  }

  Future<RatingCourseDetailModel> getRatingDetailCourse(String id) async {
    Uri url = Uri.parse('$baseUrl/homepage/course/rating/$id');
    http.Response response = await http.get(url, headers: baseHeader);
    if (response.statusCode == 200) {
      return RatingCourseDetailModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Data Rating Detail Kursus Gagal Diambil');
    }
  }

  Future<MyCourseModel> getMyCourse() async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/course/my');
    http.Response response =
        await http.get(url, headers: headerWithToken(token!));
    if (response.statusCode == 200) {
      print('getmycourse berhasil');
     print("Berhasil getmycourse ${response.body}");
      return MyCourseModel.fromJson(jsonDecode(response.body));
    } else if (response.statusCode == 404) {
      print('getmycourse eror');
      return MyCourseModel(data: [[]]);
    } else {
      print('getmycourse gagal');
      throw Exception('Data Kursus Saya, Gagal Diambil');
    }
  }

  Future<MyCourseModel> getSearchMyCourse(String courseName) async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse(
        '$baseUrl/users/course/my_course_keyword_search?keyword=$courseName');
    http.Response response =
        await http.get(url, headers: headerWithToken(token!));
    if (response.statusCode == 200) {
      print(response.body);
      return MyCourseModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Data Kursus Saya, Gagal Diambil');
    }
  }

  Future<List> postingReviewCourse(
      String review, int courseId, int valueRating) async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/review');
    var body = jsonEncode(
        {"rating": valueRating, "course_id": courseId, "review": review});
    http.Response response =
        await http.post(url, headers: headerWithToken(token!), body: body);

    if (response.statusCode == 201 || response.statusCode == 200) {
      return [true, response.statusCode];
    } else {
      return [false, 400];
    }
  }

  Future<List<CourseModel>> getTopCourse() async {
    Uri url = Uri.parse('$baseUrl/homepage/courses?top_course=1');

    http.Response response = await http.get(url, headers: baseHeader);

    if (response.statusCode == 200) {
      var data = jsonDecode(response.body)['data'][0];
      List<CourseModel> course = [];
      for (var item in data) {
        course.add(CourseModel.fromJson(item));
      }

      return course;
    } else {
      throw Exception('Data Kursus Teratas Gagal Diambil');
    }
  }

  Future<List<CourseModel>> getPromoCourse() async {
    Uri url = Uri.parse('$baseUrl/homepage/promo-courses');

    http.Response response = await http.get(url, headers: baseHeader);

    if (response.statusCode == 200) {
      var data = jsonDecode(response.body)['data'][0];
      List<CourseModel> course = [];
      print("Berhasil data promo${response.body}");
      for (var item in data) {
        course.add(CourseModel.fromJson(item));
      }

      return course;
    }else if (response.statusCode == 404) {
      print("Promo kosong, tidak ada promo. ${response.body}");
      return [];
    } else {
      throw Exception('Data Kursus Promo Gagal Diambil');
    }
  }

  Future<List<CourseModel>> getLatestCourse() async {
    Uri url = Uri.parse('$baseUrl/mobile/newcourse');

    http.Response response = await http.get(url, headers: baseHeader);
    if (response.statusCode == 200) {
      var data = jsonDecode(response.body)['data'][0];
      List<CourseModel> course = [];
      for (var item in data) {
        course.add(CourseModel.fromJson(item));
      }
      return course;
    } else {
      throw Exception('Data Kursus Teratas Gagal Diambil');
    }
  }
}
