import 'package:satu/core/base/base_service.dart';
import 'package:satu/core/entity/category_entity.dart';
import 'package:satu/core/entity/goods_entity.dart';
import 'package:satu/core/models/dictionary/category/category_response.dart';
import 'package:satu/core/models/dictionary/contragent/contragent_response_entity.dart';
import 'package:satu/core/models/dictionary/good/good_response_entity.dart';
import 'package:satu/core/models/dictionary/response/dict_response_entity.dart';
import 'package:satu/core/redux/store.dart';
import 'package:satu/core/utils/locator.dart';

import 'api_service.dart';
import 'db_service.dart';

class DictionaryService extends BaseService {
  final ApiService _api = locator<ApiService>();
  final DbService _db = locator<DbService>();

  Future<void> refreshFull() async {}

  Future<List<Category>> getCategoryByParentId(int parentId) async {
    final List<Category> list = [];
    try {
      final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId;
      final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(
          categoryTableName,
          '$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?',
          [appCompanyId, parentId]);
      for (final Map<String, dynamic> element in elements) {
        list.add(Category.fromMap(element));
      }
    } catch (e, stack) {
      log.e('getCategoryByParentId', e, stack);
    }
    return list;
  }

  Future<Category?> getCategoryById(int id) async {
    Category? result;
    try {
      final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId;
      final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(
          categoryTableName,
          '$categoryColumnAppCompanyId = ? and $categoryColumnId = ?',
          [appCompanyId, id]);

      for (final Map<String, dynamic> element in elements) {
        result = Category.fromMap(element);
      }
    } catch (e, stack) {
      log.e('getCategoryById', e, stack);
    }
    return result;
  }

  Future<List<Good>> getGoodsByCategoryId(int categoryId) async {
    final List<Good> list = [];
    try {
      final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId;
      final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(
          goodTableName,
          '$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?',
          [appCompanyId, categoryId]);
      for (final Map<String, dynamic> element in elements) {
        list.add(Good.fromMap(element));
      }
    } catch (e, stack) {
      log.e('getGoodsByCategoryId', e, stack);
    }
    return list;
  }

  Future<List<Good>> getGoodsByNameOrEan(String query) async {
    final List<Good> list = [];
    try {
      final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId;
      String where =
          '( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) ';
      final List args = [appCompanyId, '%$query%'];
      if (_isNumericInt(query)) {
        where += ' or $GoodColumnEan like ?';
        args.add('${int.parse(query).toString()}%');
      }
      final List<Map<String, dynamic>> elements =
          await _db.queryRowsWithWhere(goodTableName, where, args);
      for (final Map<String, dynamic> element in elements) {
        list.add(Good.fromMap(element));
      }
    } catch (e, stack) {
      log.e('getGoodsByCategoryId', e, stack);
    }
    return list;
  }

  bool _isNumericInt(String? str) {
    if (str == null) {
      return false;
    }
    return int.tryParse(str) != null;
  }

  Future<List<CategoryResponse>> getCategories(
      {required int page, required int perpage, String? query}) async {
    List<CategoryResponse> list = [];
    try {
      final Map<String, dynamic> requestBody = <String, dynamic>{
        'page': page,
        'perpage': perpage,
        'filter': [
          {'col': 'name', 'action': 'like', 'val': query ?? ''}
        ]
      };

      DictResponseEntity categories = await _api
          .dictionaries('/goods_categories_get', requestBody: requestBody);
      if (categories.original.data != null &&
          categories.original.data!.isNotEmpty) {
        for (final dynamic map in categories.original.data!) {
          final CategoryResponse good = CategoryResponse.fromJson(map);
          list.add(good);
        }
      }
    } catch (e, stack) {
      log.e('getCategories', e, stack);
    }
    return list;
  }

  Future<String?> saveCategory(CategoryResponse category) async {
    DictResponseEntity? status;
    log.i(category.toJson());
    if (category.id != null) {
      status = await _api.dictionarySave(
          '/goods_categories_edit', category.toJson());
    } else {
      status =
          await _api.dictionarySave('/goods_categories_add', category.toJson());
    }
    String? message = getErrorMsg(status);

    return message;
  }

  Future<String?> deleteCategory(CategoryResponse category) async {
    DictResponseEntity status = await _api.dictionarySave(
        '/goods_categories_delete', category.toJson());
    String? message = getErrorMsg(status);
    return message;
  }

  Future<List<GoodResponseEntity>> getGoods(
      {required int page, required int perpage, String? query}) async {
    List<GoodResponseEntity> list = [];
    try {
      final Map<String, dynamic> requestBody = <String, dynamic>{
        'page': page,
        'perpage': perpage,
        'filter': [
          {'col': 'name', 'action': 'like', 'val': query ?? ''}
        ]
      };

      DictResponseEntity categories =
          await _api.dictionaries('/goods_goods_get', requestBody: requestBody);
      if (categories.original.data != null &&
          categories.original.data!.isNotEmpty) {
        for (final dynamic map in categories.original.data!) {
          final GoodResponseEntity good = GoodResponseEntity.fromJson(map);
          list.add(good);
        }
      }
    } catch (e, stack) {
      log.e('getGoods', e, stack);
    }
    return list;
  }

  Future<String?> saveGood(GoodResponseEntity good) async {
    DictResponseEntity? status;
    if (good.id != null) {
      status =
          await _api.dictionarySave('/goods_goods_edit', good.toJson());
    } else {
      status =
          await _api.dictionarySave('/goods_goods_add', good.toJson());
    }
    String? message = getErrorMsg(status);

    return message;
  }

  Future<String?> deleteGood(GoodResponseEntity good) async {
    DictResponseEntity status =
        await _api.dictionarySave('/goods_goods_delete', good.toJson());
    String? message = getErrorMsg(status);
    return message;
  }

  Future<List<ContragentResponseEntity>> getContragents(
      {int? page, int? perpage, String? query}) async {
    final List<ContragentResponseEntity> list = [];
    try {
      final Map<String, dynamic> requestBody = <String, dynamic>{
        'page': page,
        'perpage': perpage,
        'filter': [
          {'col': 'name', 'action': 'like', 'val': query ?? ''}
        ]
      };
      final DictResponseEntity contragents = await _api
          .dictionaries('/general_contragents_get', requestBody: requestBody);
      if (contragents.original.data != null) {
        if (contragents.original.data!.isNotEmpty) {
          for (dynamic map in contragents.original.data!) {
            list.add(ContragentResponseEntity.fromJson(map));
          }
        }
      }
    } catch (e, stack) {
      log.e('getContragents', e, stack);
    }
    return list;
  }

  Future<String?> saveContragent(ContragentResponseEntity contragent) async {
    DictResponseEntity? status;
    if (contragent.id != null) {
      status = await _api.dictionarySave(
          '/general_contragents_edit', contragent.toJson());
    } else {
      status = await _api.dictionarySave(
          '/general_contragents_add', contragent.toJson());
    }
    String? message = getErrorMsg(status);

    return message;
  }

  Future<String?> deleteContragent(ContragentResponseEntity contragent) async {
    DictResponseEntity status = await _api.dictionarySave(
        '/general_contragents_delete', contragent.toJson());
    String? message = getErrorMsg(status);
    return message;
  }

  String? getErrorMsg(DictResponseEntity status) {
    String? message;

    if (status.exception != null) {
      message = status.exception;
    } else if (status.original.message != null) {
      message = status.original.message;
    } else if (status.original.errors != null) {
      Map<String, List<String>> errors = status.original.errors!;
      errors.values.forEach((element) {
        message = '${message ?? ''} \n ${element.join(',')}';
      });
    }
    return message;
  }
}
