import 'package:fpdart/fpdart.dart';
import 'package:kaer_with_panels/app/services/api_service/api.dart';
import 'package:kaer_with_panels/app/model/response/kr_web_text.dart';
import 'package:kaer_with_panels/app/network/base_response.dart';
import 'package:kaer_with_panels/app/network/http_error.dart';
import 'package:kaer_with_panels/app/network/http_util.dart';
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';

/// 网页相关 API
class KRWebApi {
  /// 获取网页文本内容
  Future<Either<HttpError, String>> kr_getWebText(String url) async {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['url'] = url;

    BaseResponse<KRWebText> baseResponse = await HttpUtil.getInstance().request<KRWebText>(
      url,
      data,
      method: HttpMethod.GET,
      isShowLoading: false,
    );

    if (!baseResponse.isSuccess) {
      return left(HttpError(msg: baseResponse.retMsg, code: baseResponse.retCode));
    }

    // 根据 URL 返回对应的内容
    if (url.contains(Api.kr_getSitePrivacy)) {
      return right(baseResponse.model.privacyPolicy);
    } else if (url.contains(Api.kr_getSiteTos)) {
      return right(baseResponse.model.tosContent);
    } else {
      return right(baseResponse.model.privacyPolicy); // 默认返回隐私政策
    }
  }

  /// 获取网页内容
  // Future<Either<String, String>> kr_getWebContent() async {
  //   try {
  //     // ... 其他代码 ...
  //   } catch (e) {
  //     KRLogUtil.kr_e('获取网页内容失败: $e', tag: 'WebApi');
  //     return Left('获取网页内容失败: $e');
  //   }
  // }

  // /// 获取网页内容
  // Future<Either<String, String>> kr_getWebContentWithRetry() async {
  //   try {
  //     // ... 其他代码 ...
  //   } catch (e) {
  //     KRLogUtil.kr_e('获取网页内容失败: $e', tag: 'WebApi');
  //     return Left('获取网页内容失败: $e');
  //   }
  // }
} 