import 'package:fis_common/index.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/managers/interfaces/entities/media_library.dart';

import 'library_info.dart';
import 'media_library/media_library_review.dart';

class MediaLibrarysCard extends FStatefulWidget implements FPage {
  final MediaLibrary mediaLibrary;
  final double size;
  MediaLibrarysCard(this.mediaLibrary, {this.size = 130});

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

  @override
  String get pageName => "MediaLibrarysCard";
}

class MediaLibrarysCardState extends FState<MediaLibrarysCard> {
  MediaLibrary get mediaLibrary => widget.mediaLibrary;
  double get size => widget.size;
  bool _isHovering = false;
  @override
  FWidget build(BuildContext context) {
    return FColumn(
      children: [
        FInkWell(
          onTap: () {
            router.dialog(
                () => MediaLibraryReview(widget.mediaLibrary.fileToken));
          },
          child: FMouseRegion(
            cursor: SystemMouseCursors.click,
            child: FContainer(
              width: size,
              height: size,
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.grey[200]!,
                ),
                borderRadius: BorderRadius.all(
                  Radius.circular(8),
                ),
                color: _isHovering ? Colors.grey[200] : Colors.transparent,
              ),
              child: getFileIcon(),
            ),
          ),
        ),
        const FSizedBox(height: 10),
        FTextTooltip(
          position: DisplayPosition.bottom,
          textStyle: TextStyle(fontSize: 16, color: Colors.white),
          height: 36,
          message: mediaLibrary.name,
          child: FInkWell(
            onTap: () {
              router.dialog(() => LibraryInfo(widget.mediaLibrary));
            },
            onHover: (value) {
              setState(() {
                _isHovering = value;
              });
            },
            child: FMouseRegion(
              cursor: SystemMouseCursors.click,
              child: FContainer(
                width: size,
                child: FText(
                  mediaLibrary.name,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ),
          ),
          margin: EdgeInsets.all(0),
          decoration: BoxDecoration(
            color: Colors.black.withOpacity(1.0),
            borderRadius: BorderRadius.circular(4),
          ),
        ),
      ],
    );
  }

  FWidget getFileIcon() {
    FWidget child;
    if (mediaLibrary.previewFileToken.isNotNullOrEmpty) {
      return FImage.network(
        mediaLibrary.previewFileToken!,
        height: size,
        width: size,
      );
    }
    switch (mediaLibrary.fileType) {
      case UploadFileTypeEnum.JPEG:
      case UploadFileTypeEnum.JPG:
      case UploadFileTypeEnum.ICON:
      case UploadFileTypeEnum.PNG:
      case UploadFileTypeEnum.GIF:
      case UploadFileTypeEnum.WEBP:
      case UploadFileTypeEnum.TIFF:
      case UploadFileTypeEnum.IMG:
        child = FClipRRect(
          borderRadius: BorderRadius.circular(8), // 设置圆角为8
          child: FImage.network(
            mediaLibrary.fileToken,
            height: size,
            width: size,
          ),
        );

        break;
      case UploadFileTypeEnum.PDF:
        child = FIcon(
          FIcons.ppt,
          size: size,
          color: Colors.red,
        );
        break;
      case UploadFileTypeEnum.DOC:
      case UploadFileTypeEnum.DOCX:
        child = FIcon(
          FIcons.word,
          color: Colors.blue,
          size: size,
        );
        break;
      case UploadFileTypeEnum.PPT:
      case UploadFileTypeEnum.PPTX:
        child = FIcon(
          FIcons.ppt,
          size: size,
          color: Colors.yellow,
        );
        break;
      case UploadFileTypeEnum.XLS:
      case UploadFileTypeEnum.XLSX:
        child = FIcon(
          FIcons.excel,
          size: size,
          color: Colors.green,
        );
        break;
      case UploadFileTypeEnum.MP4:
      case UploadFileTypeEnum.M4V:
        child = FIcon(
          Icons.video_file_outlined,
          size: size,
          color: Colors.lightBlue,
        );
        break;
      // case FileType.WAV:
      //   child = FIcon(
      //     FIcons.audio_file,
      //     size: size,
      //     color: Color.fromARGB(255, 255, 129, 10),
      //   );
      //   break;
      default:
        child = FSizedBox();
        break;
    }
    return child;
  }
}
