import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:get/get.dart';
import 'package:daniulive/views/play_view.dart';
import 'package:daniulive/views/merge_play_view/main_view.dart';
import '../controller.dart';

class DaniuDeviceLivePanel extends FStatelessWidget {
  DaniuDeviceLivePanel() {
    controller = Get.find<DeviceLiveDetailMobileController>();
  }
  late final DeviceLiveDetailMobileController controller;

  @override
  FWidget build(BuildContext context) {
    return QuickFWidget(
      Obx(
        () {
          if (controller.state.isRtmpUseMerge) {
            return Center(
              child: AspectRatio(
                aspectRatio: controller.state.rtmpMainViewWidth /
                    controller.state.rtmpMainViewHeight,
                child: MergePlayMainView(
                  width: controller.state.rtmpMainViewWidth,
                  height: controller.state.rtmpMainViewHeight,
                  userId: "DeviceMainChannel",
                  controller: controller.mergePlayController!,
                  onViewCreated: (value) {
                    print("onViewCreated: $value");
                    controller.state.isDaniuLiveFirstFrameRendered = false;
                    controller.initDaniuPlayerAfterViewCreated();
                    // controller.mainChannelPlayController!.updateReadyState(true);
                    // widget.channel.viewId = value;
                  },
                  onFirstFrameRendered: (width, height) {
                    print("onFirstFrameRendered: $width, $height");
                    controller.state.isDaniuLiveFirstFrameRendered = true;
                    if (controller.state.isSingleChannel) {
                      if (controller.state.rtmpMainViewWidth != width ||
                          controller.state.rtmpMainViewHeight != height) {
                        controller.state.rtmpMainViewWidth = width;
                        controller.state.rtmpMainViewHeight = height;
                      }
                    }
                  },
                  onVideoLost: (reason) async {
                    print("onVideoLost: $reason");
                    final result = await controller.checkLiveDeviceState();
                    if (result == 2) {
                      controller.reLoadDaniuLive();
                    }
                  },
                ),
              ),
            );
          } else {
            return Center(
              child: AspectRatio(
                aspectRatio: controller.state.rtmpMainViewWidth /
                    controller.state.rtmpMainViewHeight,
                child: PlayView(
                  controller: controller.mainViewPlayController!,
                  onViewCreated: (value) {
                    // widget.channel.viewId = value;
                    print("onViewCreated: $value");
                    controller.mainViewPlayController!.play();
                  },
                ),
              ),
            );
          }
        },
      ),
    );
  }
}
