import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:openim_common/openim_common.dart';

class ChatVoiceView extends StatelessWidget {
  final bool isISend;
  final String? soundPath;
  final String? soundUrl;
  final int? duration;
  final bool isPlaying;
  final bool isUnread;

  const ChatVoiceView({
    Key? key,
    required this.isISend,
    this.soundPath,
    this.soundUrl,
    this.duration,
    this.isPlaying = false,
    this.isUnread = false,
  }) : super(key: key);

  double get _bubbleWidth {
    final sec = duration ?? 0;
    final width = 64.0 + (sec > 0 ? (sec - 1) : 0) * 8.0;
    return width.clamp(64.0, 200.0);
  }

  Widget _buildVoiceAnimView() {
    final icon = isPlaying
        ? (ImageRes.voiceBlueAnim.toLottie
          ..height = 24.h
          ..width = 24.w
          ..fit = BoxFit.fitHeight)
        : (ImageRes.voiceBlue.toImage
          ..width = 24.w
          ..height = 24.h);
    final seconds = '${duration ?? 0}″'.toText
      ..style = Styles.ts_0C1C33_14sp;
    return isISend
        ? Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              seconds,
              8.horizontalSpace,
              RotatedBox(quarterTurns: 90, child: icon),
            ],
          )
        : Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              icon,
              8.horizontalSpace,
              seconds,
            ],
          );
  }

  @override
  Widget build(BuildContext context) {
    final bubble = ChatBubble(
      bubbleType: isISend ? BubbleType.send : BubbleType.receiver,
      child: SizedBox(
        width: _bubbleWidth,
        child: _buildVoiceAnimView(),
      ),
    );
    if (!isUnread) return bubble;
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        bubble,
        8.horizontalSpace,
        const ChatVoiceReadStatusView(),
      ],
    );
  }
}
