// ignore_for_file: prefer_interpolation_to_compose_strings

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:sapaly_shop/models/category_model.dart';
import 'package:sapaly_shop/models/details_model.dart';
import 'package:sapaly_shop/models/settings_model.dart';
import 'package:sapaly_shop/models/sliders_model.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../constants/global_variables.dart';
import '../../models/product_model.dart';
import '../../models/subcategory_model.dart';

class API {
  static Future<dynamic> _getData(String path) async {
    try {
      final response = await http.get(
        Uri.parse('$kBaseUrl/api/$path'),
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/json",
        },
      );

      if (response.statusCode == 200) {
        return jsonDecode(response.body);
      }
    } catch (e) {
      debugPrint(e.toString());
      rethrow;
    }
  }

  Future<List<ProductModel>> getProductsByCategory(int id) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String currentLang = prefs.getString('language') ?? 'ru';
    try {
      final products = await _getData('v1/products/category/$id');

      return products.containsKey('data')
          ? (products['data']['data'] as List)
              .map((e) => ProductModel.fromJson(e, currentLang))
              .toList()
          : [].cast<ProductModel>();
    } catch (e) {
      debugPrint('product by categ     ' + e.toString());
      rethrow;
    }
  }

  Future<DetailsModel> getDetails(int id) async {
    try {
      final details = await _getData("v1/products/$id");
      return DetailsModel.fromJson(details);
    } catch (e) {
      debugPrint('Details     ' + e.toString());
      rethrow;
    }
  }

  Future<List<CategoryModel>> getCategories() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String currentLanguage = prefs.getString('language') ?? 'ru';
    try {
      final categories = await _getData("v1/categories");

      return (categories["data"] as List)
          .map((item) => CategoryModel.fromJson(item, currentLanguage))
          .toList();
    } catch (e) {
      debugPrint('Category  ' + e.toString());
      rethrow;
    }
  }

  Future<List<SubcategoryModel>> getSubCategories() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String currentLanguage = prefs.getString('language') ?? 'ru';
    try {
      final categories = await _getData("v1/category/sub/19");
      return (categories["data"] as List)
          .map((item) => SubcategoryModel.fromJson(item, currentLanguage))
          .toList();
    } catch (e) {
      debugPrint('Category     ' + e.toString());
      rethrow;
    }
  }

  Future<List<SlidersModel>> getSliders() async {
    try {
      final categories = await _getData("v1/sliders");
      return (categories["data"] as List)
          .map((item) => SlidersModel.fromJson(item))
          .toList();
    } catch (e) {
      debugPrint('Slider     ' + e.toString());
      rethrow;
    }
  }
}
