import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:easycook_mobile/models/recommedation.dart';
import 'package:easycook_mobile/shared/shared_values.dart' as AppConst;
import 'package:shared_preferences/shared_preferences.dart';

class DapurkuRepositories {
  Dio dio = new Dio();

  final String _baseUrl = AppConst.BASE_URL;

  Future<void> dapurku(rawMaterial) async {
    final formData = FormData.fromMap({
      'raw_material': rawMaterial,
    });

    var response = await dio.post(
      "$_baseUrl/profiles/my-kitchen/add-raw-material",
      data: formData,
      options: Options(headers: {
        HttpHeaders.contentTypeHeader: 'application/json',
      }),
    );
  }

//FOR RECOMMENDATION

  Future<RawMaterialResponse> getRecipesForRecommendation(
      {required String nameProduct}) async {
    var response = await dio.get(
      "$_baseUrl/recipes/raw-material?name=$nameProduct",
      options: Options(headers: {
        HttpHeaders.contentTypeHeader: 'application/json',
      }),
    );

    Map<String, dynamic> data = {"data": response.data};
    return RawMaterialResponse.fromJson(jsonDecode(jsonEncode(data)));
  }

  Future<RekomendasiResepResponse> addRawMaterialForRecommendation(
      {required List<RawMaterial> rawMaterialSelected}) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var token = prefs.getString('token');
    List<int> rawMaterialSelectedId = [];

    for (var i = 0; i < rawMaterialSelected.length; i++) {
      rawMaterialSelectedId.add(rawMaterialSelected[i].id);
    }

    // FormData formData =
    //     FormData.fromMap({"raw_material_id": rawMaterialSelectedId});
    Map<String, dynamic> data = {"raw_ingredient_id": rawMaterialSelectedId};

    // print(formData);

    var response = await dio.post(
      "$_baseUrl/profiles/pantry/",
      data: data,
      options: Options(headers: {
        HttpHeaders.contentTypeHeader: 'application/json',
        HttpHeaders.authorizationHeader: 'Token $token',
      }),
    );

    Map<String, dynamic> dataResult = {"data": response.data};
    return RekomendasiResepResponse.fromJson(
        jsonDecode(jsonEncode(dataResult)));
  }
}
