import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/values/spacings.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/views/common/device_live/device_live_list/index.dart';
import 'package:get/get.dart';

import 'state.dart';

class DeviceLiveListMobileController extends DeviceLiveListController {
  @override
  final DeviceLiveListMobileState state = DeviceLiveListMobileState();

  final searchFocusNode = FocusNode();

  @override
  void handleEnterLive(int index) async {
    final code = state.deviceList[index].deviceCode!;
    final dto = await deviceManager.getDeviceInfo(code);
    if (dto == null) {
      return;
    }
    if (dto.isOnline) {
      _gotoDetail(code);
    } else {
      PromptBox.toast(i18nBook.live.tipsOfDeviceOffline.t);
    }
  }

  void _gotoDetail(String code) async {
    enterFullScreen();

    await Get.toNamed(
      MobileRouteNames.Live.DeviceDetail,
      parameters: {"code": code},
    );

    exitFullScreen();
  }

  @override
  void handleShare(int index) async {
    final code = getCodeByIndex(index);
    final url = await deviceManager.shareDeviceLive(code);
    if (url != null && url.isNotEmpty) {
      await showModalBottomSheet(
        context: Get.context!,
        backgroundColor: Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(8),
            topRight: Radius.circular(8),
          ),
        ),
        isScrollControlled: false,
        isDismissible: true,
        builder: (BuildContext context) {
          return SafeArea(
            child: Column(
              mainAxisSize: MainAxisSize.min, // 设置最小的弹出
              children: [
                const SizedBox(height: FSpacings.XXL),
                ListTile(
                  leading: Icon(Icons.copy),
                  title: Text(i18nBook.remedical.copyLink.t),
                  onTap: () {
                    Clipboard.setData(ClipboardData(text: url));
                    PromptBox.toast(i18nBook.remedical.copySuccess.t);
                    router.back();
                  },
                ),
                const SizedBox(height: FSpacings.XXL),
              ],
            ),
          );
        },
      );
    }
  }
}
