import 'dart:ui';

import 'package:flutter/foundation.dart';
import '../models/annotation.dart';

/// 批注状态管理
class AnnotationProvider with ChangeNotifier {
  List<Annotation> _annotations = [];
  bool _loading = false;

  List<Annotation> get annotations => _annotations;
  bool get loading => _loading;

  /// 加载批注列表
  Future<void> loadAnnotations({String? documentId}) async {
    _loading = true;
    notifyListeners();

    // 模拟网络请求
    await Future.delayed(const Duration(milliseconds: 300));

    // 模拟批注数据
    _annotations = _getMockAnnotations(documentId);

    _loading = false;
    notifyListeners();
  }

  /// 添加批注
  void addAnnotation(Annotation annotation) {
    _annotations.add(annotation);
    notifyListeners();
  }

  /// 更新批注
  void updateAnnotation(Annotation annotation) {
    final index = _annotations.indexWhere((a) => a.id == annotation.id);
    if (index != -1) {
      _annotations[index] = annotation;
      notifyListeners();
    }
  }

  /// 删除批注
  void removeAnnotation(String id) {
    _annotations.removeWhere((a) => a.id == id);
    notifyListeners();
  }

  /// 接受批注（应用建议）
  void acceptAnnotation(String id) {
    final annotation = _annotations.firstWhere((a) => a.id == id);
    // 这里可以触发文本替换逻辑
    removeAnnotation(id);
  }

  /// 拒绝批注
  void rejectAnnotation(String id) {
    removeAnnotation(id);
  }

  /// 根据文档ID获取批注
  List<Annotation> getAnnotationsByDocumentId(String? documentId) {
    if (documentId == null) return _annotations;
    return _annotations.where((a) => a.documentId == documentId).toList();
  }

  /// 模拟批注数据
  List<Annotation> _getMockAnnotations(String? documentId) {
    return [
      // 错别字批注
      Annotation(
        id: 'a1',
        text: '智能票据',
        start: const Offset(0, 0),
        end: const Offset(100, 20),
        type: AnnotationType.strikethrough,
        suggestion: '智能票据处理',
        documentId: documentId,
        createdAt: DateTime.now(),
      ),
      // AI润色建议
      Annotation(
        id: 'a2',
        text: '提升发票、合同、报销单据等票据类文档的处理效率与准确率',
        start: const Offset(0, 0),
        end: const Offset(200, 20),
        type: AnnotationType.suggestion,
        suggestion: '建议：可以优化为"显著提升处理效率与准确率"',
        documentId: documentId,
        createdAt: DateTime.now(),
      ),
    ];
  }
}
