import 'package:dio/dio.dart';
import 'mock_data.dart';

class MockInterceptor extends Interceptor {
  final bool enabled;

  MockInterceptor({this.enabled = false});

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    if (!enabled) {
      handler.next(options);
      return;
    }

    final path = options.path;
    final params = options.queryParameters;
    final mockData = _matchMock(path, params);

    if (mockData != null) {
      Future.delayed(const Duration(milliseconds: 300), () {
        handler.resolve(Response(
          requestOptions: options,
          statusCode: 200,
          data: mockData,
        ));
      });
    } else {
      handler.resolve(Response(
        requestOptions: options,
        statusCode: 200,
        data: {'code': 0, 'message': 'ok', 'data': null},
      ));
    }
  }

  Map<String, dynamic>? _matchMock(
      String path, Map<String, dynamic> params) {
    final status = params['status'] as String? ?? '';
    final page = params['page'] as String? ?? '1';

    if (path == '/home/summary') return MockData.homeSummary;

    if (path == '/expense/list') {
      var list = MockData.expenseList;
      if (status.isNotEmpty) {
        list = list.where((e) => e['status'] == status).toList();
      }
      return _paginate(list, page);
    }
    if (path.startsWith('/expense/detail/')) {
      return MockData.expenseDetail(path.split('/').last);
    }
    if (path == '/expense/apply' || path == '/expense/draft') {
      return MockData.success;
    }

    if (path == '/expense-apply/list') {
      var list = MockData.expenseApplicationList;
      if (status.isNotEmpty) {
        list = list.where((e) => e['status'] == status).toList();
      }
      return _paginate(list, page);
    }
    if (path.startsWith('/expense-apply/detail/')) {
      return MockData.expenseApplicationDetail(path.split('/').last);
    }
    if (path == '/expense-apply/submit' || path == '/expense-apply/draft') {
      return MockData.success;
    }

    if (path == '/overtime/list') {
      var list = MockData.overtimeList;
      if (status.isNotEmpty) {
        list = list.where((e) => e['status'] == status).toList();
      }
      return _paginate(list, page);
    }
    if (path.startsWith('/overtime/detail/')) {
      return MockData.overtimeDetail(path.split('/').last);
    }
    if (path == '/overtime/apply' || path == '/overtime/draft') {
      return MockData.success;
    }

    if (path == '/vehicle/list') {
      var list = MockData.vehicleList;
      if (status.isNotEmpty) {
        list = list.where((e) => e['status'] == status).toList();
      }
      return _paginate(list, page);
    }
    if (path.startsWith('/vehicle/detail/')) {
      return MockData.vehicleDetail(path.split('/').last);
    }
    if (path == '/vehicle/apply' || path == '/vehicle/draft') {
      return MockData.success;
    }

    if (path == '/outing-log/list') {
      return _paginate(MockData.outingLogList, page);
    }
    if (path.startsWith('/outing-log/detail/')) {
      return MockData.outingLogDetail(path.split('/').last);
    }
    if (path == '/outing-log/create') return MockData.success;

    if (path == '/announcement/list') {
      return _paginate(MockData.announcementList, page);
    }
    if (path.startsWith('/announcement/detail/')) {
      return MockData.announcementDetail(path.split('/').last);
    }

    if (path.startsWith('/report/expense-detail/')) {
      return MockData.expenseReportDetail(path.split('/').last);
    }
    if (path.startsWith('/report/expense-apply-detail/')) {
      return MockData.expenseApplyReportDetail(path.split('/').last);
    }
    if (path.startsWith('/report/overtime-detail/')) {
      return MockData.overtimeReportDetail(path.split('/').last);
    }
    if (path.startsWith('/report/vehicle-detail/')) {
      return MockData.vehicleReportDetail(path.split('/').last);
    }

    return null;
  }

  Map<String, dynamic> _paginate(
      List<Map<String, dynamic>> list, String pageStr) {
    final page = int.tryParse(pageStr) ?? 1;
    const size = 20;
    return {
      'code': 0,
      'message': 'ok',
      'data': {
        'list': list,
        'page': page,
        'size': size,
        'total': list.length,
      },
    };
  }
}
