import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flyinsono/pages/components/select.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_i18n/i18n.dart';
import '../index.dart';

/// 视频列表卡片
class VideoCard extends FisView<PastVideosController> {
  const VideoCard(
      {required this.index, required this.info, required this.onTap, Key? key})
      : super(key: key);
  final int index;
  final CourseInfoDetailDTO info;
  final VoidCallback onTap;
  @override
  FWidget build(BuildContext context) {
    return FInkWell(
      onTap: onTap,
      child: FContainer(
        margin: EdgeInsets.all(5),
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.2),
              blurRadius: 5,
              spreadRadius: 0,
              offset: Offset(1, 1), // changes position of shadow
            ),
          ],
        ),
        child: FColumn(
          children: [
            FContainer(
              decoration: BoxDecoration(
                border: Border.all(
                  color: Color.fromARGB(255, 211, 211, 211),
                  width: 1,
                  strokeAlign: BorderSide.strokeAlignOutside,
                ),
                borderRadius: BorderRadius.circular(8),
              ),
              clipBehavior: Clip.antiAlias,
              child: QuickFWidget(
                AspectRatio(
                  aspectRatio: 16 / 9,
                  child: Stack(
                    children: [
                      Center(
                        child: AspectRatio(
                          aspectRatio: 16 / 9,
                          child: _buildVideoCoverImage(info.cover ?? ''),
                        ),
                      ),
                      // TODO 暂时屏蔽置顶和星级标签（需求来自群聊）
                      // if (info.isStick)
                      //   Align(
                      //     alignment: Alignment.topLeft,
                      //     child: _buildStickyLabel(),
                      //   ),
                      // if (info.sort > 0)
                      //   Align(
                      //     alignment: Alignment.bottomRight,
                      //     child: Transform(
                      //       transform: Matrix4.translationValues(5, 5, 0),
                      //       child: _buildStarLevelLabel(info.sort),
                      //     ),
                      //   ),
                    ],
                  ),
                ),
              ),
            ),
            FSizedBox(height: 10),
            FExpanded(
              child: _buildVideoInfo(
                info.name ?? '',
                _durationToTime(info.duration),
                info.createTime!.toLocal().toString().substring(0, 11),
              ),
            ),
          ],
        ),
      ),
    );
  }

  FWidget _buildVideoCoverImage(String imageUrl) {
    FImage image;
    if (imageUrl.isEmpty) {
      image = FImage.asset(
        "assets/images/logo_en_white.png",
        filterQuality: FilterQuality.low,
        fit: BoxFit.contain,
      );
    } else {
      image = FImage.network(
        imageUrl,
        colorBlendMode: BlendMode.dstOver,
        color: Colors.white,
        filterQuality: FilterQuality.low,
        fit: BoxFit.cover,
        errorBuilder: (context, error, stackTrace) {
          return FContainer(
            color: Colors.blue,
            child: FImage.asset(
              "assets/images/logo_en_white.png",
              filterQuality: FilterQuality.low,
              fit: BoxFit.contain,
            ),
          );
        },
      );
    }
    return FContainer(
        decoration: BoxDecoration(
          color: imageUrl.isEmpty ? Colors.blue : Colors.transparent,
        ),
        child: image);
  }

  /// 构建视频信息
  FWidget _buildVideoInfo(String name, String duration, String date) {
    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FContainer(
          child: OverflowDetectorText(
            child: FText(
              name,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.bold,
                color: Color.fromARGB(255, 77, 77, 77),
                fontFamily: FTheme.ins.localeSetting.fontFamily,
                fontFamilyFallback: [
                  FTheme.ins.data.localeSettings.chinese.fontFamily,
                ],
              ),
            ),
          ),
        ),
        FExpanded(
          child: FSizedBox(),
        ),
        FRow(
          children: [
            FContainer(
              child: FText(
                i18nBook.measure.playLocationDurationLabel.t,
                style: TextStyle(
                  fontSize: 14,
                  color: Color.fromARGB(255, 77, 77, 77),
                ),
              ),
            ),
            FContainer(
              child: FText(
                duration,
                style: TextStyle(
                  fontSize: 14,
                  color: Color.fromARGB(255, 120, 120, 120),
                ),
              ),
            ),
            FExpanded(
              child: FSizedBox(),
            ),
            FContainer(
              child: FText(
                date,
                style: TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.bold,
                  color: Color.fromARGB(255, 77, 77, 77),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  /// duration(min) to 00:00
  /// 1 -> 00:01
  String _durationToTime(int duration) {
    var minute = duration ~/ 60;
    var second = duration % 60;
    return "${minute.toString().padLeft(2, '0')}:${second.toString().padLeft(2, '0')}";
  }

  // 构建视频置顶标签
  FWidget _buildStickyLabel() {
    return FContainer(
      margin: EdgeInsets.only(left: 10, top: 10),
      padding: EdgeInsets.symmetric(horizontal: 5),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(5)),
        border: Border.all(color: Colors.blue),
        color: Colors.blue.withOpacity(0.8), //设置半透明蓝色
      ),
      child: FText(
        i18nBook.college.pinned.t,
        style: TextStyle(color: Colors.white),
      ),
    );
  }

  // 构建视频星级标签
  FWidget _buildStarLevelLabel(int starLevel) {
    return FContainer(
      padding: EdgeInsets.only(top: 5, left: 5, right: 10, bottom: 10),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(5)),
        border: Border.all(color: Colors.grey, width: 0.5),
        color: Colors.white, //设置半透明蓝色
      ),
      child: FRow(
        mainAxisSize: MainAxisSize.min,
        children: [
          for (var i = 0; i < 5; i++) ...[
            FIcon(
              i < starLevel ? Icons.star_rounded : Icons.star_outline_rounded,
              color: i < starLevel
                  ? Color.fromARGB(255, 235, 192, 50)
                  : Colors.grey,
              size: 18,
            ),
          ],
        ],
      ),
    );
  }
}
