import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/interfaces/entities/device_entities.dart';
import 'package:flyinsono/views/common/device_live/device_live_detail/state.dart';
import 'package:get/get.dart';
import 'package:fis_live/fis_live.dart';

import '../controller.dart';

/// 设备直播面板
class DeviceLiveBoard extends FStatefulWidget {
  const DeviceLiveBoard(this.index, {super.key});

  /// 设备数据实体
  final int index;

  @override
  FState<FStatefulWidget> createState() => _DeviceLiveBoardState();
}

class _DeviceLiveBoardState extends FState<DeviceLiveBoard> {
  /// 子画面1的位置
  Alignment _subAlignment1 = Alignment.topRight;

  late final DeviceLiveDetailDesktopController controller;
  late final DeviceStateModel model;
  late final List<DeviceLiveSourceInfoModel> sources;

  LiveController? mainChannelController;
  LiveController? camera01Controller;

  /// 是否有多路画面
  bool get hasMultiChannel => sources.length > 1;

  @override
  void initState() {
    _loadLiveData();
    super.initState();
  }

  void _loadLiveData() {
    controller = Get.find<DeviceLiveDetailDesktopController>();
    model = controller.state.deviceList[widget.index];
    final liveInfo = model.liveInfo!;
    sources = liveInfo.sources;
    //
    mainChannelController = LiveController(
      source: LiveSource(sources.first.httpFlvUrl),
    );
    if (hasMultiChannel) {
      camera01Controller = LiveController(
        source: LiveSource(sources[1].httpFlvUrl),
      );
    }
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      color: Colors.black,
      child: FStack(
        children: [
          FPositioned(
            top: 0,
            right: 0,
            bottom: 0,
            left: 0,
            child: FCenter(child: _buildMainView()),
          ),
          FPositioned(
            top: 6,
            right: 0,
            left: 0,
            child: FCenter(
              child: FText(
                model.dto.displayName!,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: FFontSizes.H6,
                ),
              ),
            ),
          ),
          if (hasMultiChannel)
            FPositioned(
              top: 0,
              right: 0,
              child: _buildSubView(0),
            ),
        ],
      ),
    );
  }

  FWidget _buildSubView(int index) {
    if (model.liveInfo!.useMerge) {
      return FSizedBox(
        width: 320,
        height: 240,
        child: FText("合图未实现", style: TextStyle(color: Colors.white)),
      );
    }
    return FContainer(
      width: 320, //TODO[melon] 计算尺寸
      height: 240,
      decoration: BoxDecoration(
        color: Colors.black,
        border: Border.all(color: Colors.grey.shade300),
      ),
      child: QuickFWidget(FLivePlayer(controller: camera01Controller!)),
    );
  }

  FWidget _buildMainView() {
    return FLayoutBuilder(
      builder: (context, constraints) {
        final layoutWidth = constraints.maxWidth;
        final layoutHeight = constraints.maxHeight;
        final layoutWHR = layoutWidth / layoutHeight;
        const diyWHR = 1280 / 720; //TODO[melon] 计算尺寸
        double w, h;
        if (layoutWHR > diyWHR) {
          h = layoutHeight;
          w = h * diyWHR;
        } else {
          w = layoutWidth;
          h = w / diyWHR;
        }
        return FContainer(
          width: w,
          height: h,
          color: Colors.black,
          child: QuickFWidget(FLivePlayer(controller: mainChannelController!)),
        );
      },
    );
  }
}
