import 'dart:convert';

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/counter_qna_comment_model.dart';
import 'package:initial_folder/models/counter_qna_like_model.dart';
import 'package:initial_folder/models/qna_model.dart';

class QnaService {
  //get QNA user
  Future<QnaModel> getMyQna(String idCourse) async {
    String? token = await UsersInfo().getToken();
    var headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    };
    Uri url = Uri.parse('$baseUrl/users/qna/$idCourse');
    http.Response response =
        await http.get(url, headers: headerWithToken(token!));
    // print(response.body);
    if (response.statusCode == 200) {
      return QnaModel.fromJson(jsonDecode(response.body));
    } else if (response.statusCode == 404) {
      return QnaModel(status: 404, error: true, data: [[]]);
    } else {
      throw Exception('Data QNA, Gagal Diambil');
    }
  }

  // Post QNA User
  Future<bool> postingQna(
    String title,
    String quest,
    String idCourse,
    String idLesson,
  ) async {
    int? idUser = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/qna');
    var body = jsonEncode({
      "sender": idUser.toString(),
      "title": title,
      "quest": quest,
      "id_lesson": idLesson,
      "id_course": idCourse,
    });
    print(body);
    http.Response response =
        await http.post(url, headers: headerWithToken(token!), body: body);
    print(response.body);

    if (response.statusCode == 201) {
      return true;
    } else {
      return false;
    }
  }

  // Update QNA User
  Future<bool> updateQna(String idCourse, String quest, int id_qna, String title, String idLesson) async {
    int? idUser = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/qna/$id_qna');
    var body = jsonEncode({
      // "sender": idUser.toString(),
      "sender": idUser,
      "quest": quest,
      "title": title,
      "id_lesson": idLesson,
      "id_course": idCourse,
    });
    print("ini Request Body update qna: $body");
    http.Response response =
        await http.put(url, headers: headerWithToken(token!), body: body);
    print(response.body);

    if (response.statusCode == 200) {
      print(response.body);
      print('berhasil update');
      return true;
    } else {
      return false;
    }
  }

  //Post Reply QNA User
  Future<bool> postQnaReply(String text_rep, String id_qna) async {
    int? idUser = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/qna/reply');
    var body = jsonEncode({
      // "sender": idUser.toString(),
      "text_rep": text_rep,
      "id_qna": id_qna,
    });
    http.Response response =
        await http.post(url, headers: headerWithToken(token!), body: body);
    print(response.statusCode);

    if (response.statusCode == 201) {
      return true;
    } else {
      return false;
    }
  }

  //Edit Reply QNA User
  Future<bool> editQnaReply(int id_rep, String text_rep, String id_qna) async {
    int? idUser = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/qna/reply/$id_rep');
    var body = jsonEncode({
      // "sender": idUser.toString(),
      "text_rep": text_rep,
      "id_qna": id_qna,
    });
    http.Response response =
        await http.put(url, headers: headerWithToken(token!), body: body);
    print(response.body);

    if (response.statusCode == 200) {
      return true;
    } else {
      return false;
    }
  }

  // Delete Qna (Pertanyaan User)
  Future<bool> deleteQna(
    int id_qna,
  ) async {
    Uri url = Uri.parse('$baseUrl/users/qna/$id_qna');
    String? token = await UsersInfo().getToken();
    print(token);

    http.Response response = await http.delete(
      url,
      headers: headerWithToken(token!),
    );
    print(response.body);
    if (response.statusCode == 200) {
      return true;
    } else {
      return false;
    }
  }

  // Delete Reply Qna User
  Future<bool> deleteReplyQna(
    int id_rep,
  ) async {
    Uri url = Uri.parse('$baseUrl/users/qna/reply/$id_rep');
    String? token = await UsersInfo().getToken();
    print(token);

    http.Response response = await http.delete(
      url,
      headers: headerWithToken(token!),
    );
    print(response.body);
    if (response.statusCode == 200) {
      return true;
    } else {
      return false;
    }
  }

  //get counter comment qna
  Future<CounterCommentModel> getCounterComment(String idQna) async {
    String? token = await UsersInfo().getToken();

    Uri url = Uri.parse('$baseUrl/users/qna/count/$idQna');
    http.Response response =
        await http.get(url, headers: headerWithToken(token!));
    print(response.body);

    if (response.statusCode == 200) {
      return CounterCommentModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Data Counter Comment QNA, Gagal Diambil');
    }
  }

  // Like or Unlike
  Future<bool> likeOrLike(int idQna) async {
    int? idUser = await UsersInfo().getIdUser();
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/qna/up/$idQna');
    // var body = jsonEncode({"user_id": idUser.toString()});
    // http.Response response =
    //     await http.post(url, headers: headerWithToken(token!), body: body);
    http.Response response =
        await http.post(url, headers: headerWithToken(token!));
    print(response.body);

    if (response.statusCode == 200) {
      return true;
    } else if (response.statusCode == 400) {
      Uri url = Uri.parse('$baseUrl/users/qna/unup/$idQna');
      http.Response response =
          await http.post(url, headers: headerWithToken(token!));

      if (response.statusCode == 200) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }

  //get counter comment qna
  Future<CounterLikeModel> getCounterLike(String idQna) async {
    String? token = await UsersInfo().getToken();
    Uri url = Uri.parse('$baseUrl/users/qna/like/$idQna');
    http.Response response =
        await http.get(url, headers: headerWithToken(token!));
    print(response.body);

    if (response.statusCode == 200) {
      return CounterLikeModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Data Counter Like QNA, Gagal Diambil');
    }
  }
}
