import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:fis_ui/values/spacings.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/interfaces/entities/consultation_member.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/controller.dart';
import 'package:get/get.dart';

import 'member_operates.dart';

class PreviewMemberCallingStatus extends FStatelessWidget
    implements FInteractiveContainer {
  @override
  String get pageName => 'PreviewMemberCallingStatus';
  final ConsultationMember member;
  final TextStyle _previewMemberTextStyle = TextStyle(color: Colors.grey);
  final controller = Get.find<LiveConsultationController>();
  PreviewMemberCallingStatus(this.member);
  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      var status = controller.state.memberStatus;
      if (!status.containsKey(member.id)) {
        return FSizedBox();
      }
      var consultationMember = status[member.id];
      if ([
        MemberStatus.Accepted,
        MemberStatus.Joined,
      ].contains(consultationMember!.status)) {
        return MemberAudioOperates(
          RoomMember(
            member.id!,
            mute: member.mute,
            videoOpend: member.videoOpend,
          ),
          channelInfo: controller.state.playChannels[member.id],
          setAudioMute: controller.setAudioMute,
          setVideoOpend: controller.setVideoOpend,
          setMicrophone: controller.setMicrophone,
        );
      } else if (consultationMember.status == MemberStatus.Calling) {
        return _cancelCallingView(member);
      } else {
        return _callAgainView(member);
      }
    });
  }

  //再次呼叫视图
  FWidget _callAgainView(ConsultationMember member) {
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
        name: 'callAgain',
        onTap: () {
          controller.callAgain(member.id ?? '');
        },
        businessParent: this,
        child: FRow(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            const FSizedBox(width: 5),
            FText(
              i18nBook.realTimeConsultation.callAgain.t,
              style: _previewMemberTextStyle,
            ),
            FIcon(
              Icons.refresh,
              color: Colors.grey,
              size: 16,
            ),
          ],
        ),
      ),
    );
  }

  ///取消呼叫视图
  FWidget _cancelCallingView(ConsultationMember member) {
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
          onTap: () {
            controller.cancelCall(member.id ?? '');
          },
          businessParent: this,
          name: 'cancel calling',
          child: FRow(
            children: [
              const FSizedBox(width: FSpacings.L),
              FText(
                i18nBook.realTimeConsultation.cancelCall.t,
                style: _previewMemberTextStyle,
              ),
              FIcon(
                Icons.close,
                color: Colors.white,
              ),
            ],
          )),
    );
  }
}
