import 'package:flutter/material.dart';
import 'package:get/get.dart';
// import 'package:crisp_sdk/crisp_sdk.dart';  // 暂时注释掉
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../controllers/kr_crisp_controller.dart';
import 'package:kaer_with_panels/app/localization/app_translations.dart';
import 'package:kaer_with_panels/app/widgets/kr_app_text_style.dart';
import '../../../widgets/kr_simple_loading.dart';

/// Crisp 客服聊天视图
class KRCrispView extends GetView<KRCrispController> {
  const KRCrispView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        await controller.kr_cleanupResources();
        return true;
      },
      child: Scaffold(
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        appBar: _kr_buildAppBar(context),
        body: _kr_buildBody(context),
      ),
    );
  }

  /// 构建导航栏
  PreferredSizeWidget _kr_buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: Theme.of(context).cardColor,
      elevation: 0,
      title: Text(
        AppTranslations.kr_userInfo.customerService,
        style: KrAppTextStyle(
          color: Theme.of(context).textTheme.bodyMedium?.color,
          fontSize: 16,
          fontWeight: FontWeight.w500,
        ),
      ),
      leading: IconButton(
        icon: Icon(
          Icons.arrow_back_ios,
          size: 20.sp,
          color: Theme.of(context).textTheme.bodyMedium?.color,
        ),
        onPressed: () => _kr_handleBack(),
      ),
    );
  }

  /// 构建主体内容
  Widget _kr_buildBody(BuildContext context) {
    return Container(
      color: Theme.of(context).scaffoldBackgroundColor,
      child: Obx(() {
        if (controller.kr_isLoading.value) {
          return _kr_buildLoadingView(context, '');
        }

        if (controller.kr_isInitialized.value && controller.crispController != null) {
          // return CrispView(
          //   crispController: controller.crispController!,
          //   clearCache: true,
          //   onSessionIdReceived: _kr_onSessionIdReceived,
          // );
          return _kr_buildPlaceholderView(context);
        }

        return _kr_buildLoadingView(context, '');
      }),
    );
  }

  /// 构建加载视图
  Widget _kr_buildLoadingView(BuildContext context, String message) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          KRSimpleLoading(
            color: Colors.blue,
            size: 50.0,
          ),
          if (message.isNotEmpty) SizedBox(height: 16.sp),
          if (message.isNotEmpty) Text(
            message,
            style: KrAppTextStyle(
              fontSize: 14,
              color: Theme.of(context).textTheme.bodyMedium?.color,
            ),
          ),
        ],
      ),
    );
  }

  /// 处理返回事件
  Future<void> _kr_handleBack() async {
    await controller.kr_cleanupResources();
    Get.back();
  }

  /// 构建占位视图
  Widget _kr_buildPlaceholderView(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.chat_bubble_outline,
            size: 64.sp,
            color: Colors.grey,
          ),
          SizedBox(height: 16.sp),
          Text(
            '客服功能暂时不可用',
            style: KrAppTextStyle(
              fontSize: 16,
              color: Colors.grey,
            ),
          ),
        ],
      ),
    );
  }

  /// 处理会话 ID 接收事件
  void _kr_onSessionIdReceived(String sessionId) {
    debugPrint('Crisp 会话 ID: $sessionId');
  }
} 