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

class LibraryInfo extends FStatefulWidget implements FPage {
  final MediaLibrary mediaLibrary;

  LibraryInfo(this.mediaLibrary) {}

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

  @override
  String get pageName => 'LibraryInfo';
}

class FLibraryInfoState extends FState<LibraryInfo> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _focusNode = FocusNode();
  final flyinsonoCollegeManager = Get.find<IFlyinsonoCollegeManager>();
  final size = 130.0;
  MediaLibrary get mediaLibrary => widget.mediaLibrary;
  String _extendName = '';

  @override
  void initState() {
    var fileName = mediaLibrary.name;
    if (fileName.contains('.')) {
      // 获取文件名（不带扩展名）
      fileName = fileName
          .split('.')
          .sublist(0, fileName.split('.').length - 1)
          .join('.');
    }
    _controller.text = fileName;
    _extendName = mediaLibrary.name.split('.').last;
    _focusNode.addListener(() {
      setState(() {});
    });
    super.initState();
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      title: FText(
        i18nBook.college.materialDetail.t,
        style: TextStyle(color: Colors.white),
      ),
      isDefault: true,
      onCancel: router.back,
      onClose: router.back,
      onOk: () async {
        var result = await flyinsonoCollegeManager.changeVideoName(
            _controller.text + '.' + _extendName, mediaLibrary.fileId!);
        if (result) {
          PromptBox.toast(i18nBook.user.operationSuccess.t);
          router.back();
          flyinsonoCollegeManager.onUpdateLibrary.emit(this, true);
        }
      },
      children: [
        FContainer(
          width: size,
          height: size,
          margin: EdgeInsets.symmetric(vertical: 20),
          child: getFileIcon(),
        ),
        const FSizedBox(height: 20),
        FRow(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            FContainer(
              margin: EdgeInsets.only(bottom: 10),
              alignment: Alignment.center,
              child: FText(i18nBook.college.fileName.t),
            ),
            FTextTooltip(
              position: DisplayPosition.top,
              textStyle: TextStyle(fontSize: 16, color: Colors.white),
              height: 36,
              message: _controller.text + '.' + _extendName,
              child: FContainer(
                width: 380,
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: 15, right: 15, bottom: 10),
                child: FTextField(
                  controller: _controller,
                  focusNode: _focusNode,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    contentPadding:
                        EdgeInsets.symmetric(vertical: 0.0, horizontal: 5.0),
                  ),
                ),
              ),
              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: 130,
        width: 130,
      );
    }
    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;
      default:
        child = FSizedBox();
        break;
    }
    return child;
  }
}
