import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/rpc.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/entitys/play_channel.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/widgets/volume_slider.dart';
import 'package:fis_common/env/env.dart';
import 'package:fis_jsonrpc/services/platform.dart';

class MemberAudioOperates extends FStatefulWidget
    implements FInteractiveContainer {
  final RoomMember member;

  final PlayChannelInfo? channelInfo;
  final void Function(bool, String) setAudioMute;
  final Future<bool> Function(bool) setVideoOpend;
  final Future<bool> Function(bool) setMicrophone;

  MemberAudioOperates(
    this.member, {
    this.channelInfo,
    required this.setAudioMute,
    required this.setVideoOpend,
    required this.setMicrophone,
  });
  @override
  String get pageName => "MemberOperate";

  @override
  FState<FStatefulWidget> createState() {
    return _MemberOperateState();
  }
}

class _MemberOperateState extends FState<MemberAudioOperates> {
  bool loudspeaker = true;
  bool _isMute = false;
  int _initVolume = 100;

  @override
  void initState() {
    _isMute = widget.member.mute;
    initMicInitVolume();
    super.initState();
  }

  void initMicInitVolume() async {
    if (kIsWeb) return;
    try {
      double micVolume =
          await rpc.platform.getAudioDeviceVolume(AudioTypeEnum.Mic);
      setState(() {
        _initVolume = (micVolume * 100).toInt();
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  void didUpdateWidget(MemberAudioOperates oldWidget) {
    if (oldWidget.member.mute != widget.member.mute) {
      setState(() {
        _isMute = widget.member.mute;
      });
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  FWidget build(BuildContext context) {
    final PlayChannelInfo? channelInfo = widget.channelInfo;
    final channel = channelInfo == null ? null : channelInfo.channel ?? null;
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        if (widget.member.id != Store.user.code) ...[
          if (channel == null) ...[
            _buildIconButton(
                loudspeaker ? Icons.volume_up_rounded : Icons.volume_off, () {
              widget.setAudioMute(loudspeaker, widget.member.id);
              if (channel != null) {
                setState(() {
                  loudspeaker = !loudspeaker;
                });
              }
            }, true),
          ] else ...[
            VolumeSlider(
              initVolume: channel.volume,
              onVolumeChanged: (value) {
                channel.setVolume(value);
              },
              child: _buildIconButton(
                  loudspeaker ? Icons.volume_up_rounded : Icons.volume_off, () {
                widget.setAudioMute(loudspeaker, widget.member.id);
                setState(() {
                  loudspeaker = !loudspeaker;
                });
              }, true),
            ),
          ],
        ],
        if (widget.member.id == Store.user.code) ...[
          _buildIconButton(
            widget.member.videoOpend
                ? Icons.video_camera_front
                : Icons.videocam_off,
            () async {
              var setResult =
                  await widget.setVideoOpend(!widget.member.videoOpend);
              if (setResult) {
                setState(() {
                  //widget.member.videoOpend = !widget.member.videoOpend;
                });
              }
            },
            false,
          ),
        ],
        _buildIconButton(
          _isMute ? Icons.mic_off : Icons.mic,
          () async {
            try {
              await rpc.platform
                  .setActiveDeviceVolume(AudioTypeEnum.Mic, _isMute ? 1 : 0);
            } catch (e) {
              print(e);
            }
            await widget.setMicrophone(!_isMute);
            setState(() {});
          },
          false,
        ),
      ],
    );
  }

  FWidget _buildIconButton(
      IconData icon, GestureTapCallback onTap, bool volume) {
    final isCanClick = volume || widget.member.id == Store.user.code;
    return FMouseRegion(
      cursor: isCanClick ? SystemMouseCursors.click : SystemMouseCursors.basic,
      child: FGestureDetector(
        child: FIcon(
          icon,
          color: isCanClick ? Colors.white : Colors.grey,
          size: 22,
        ),
        name: 'Loudspeaker',
        businessParent: widget,
        onTap: isCanClick ? onTap : null,
      ),
    );
  }
}

///成员信息
class RoomMember {
  String id;
  bool mute;
  bool videoOpend;

  RoomMember(
    this.id, {
    this.mute = false,
    this.videoOpend = true,
  });
}
