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:fis_i18n/i18n.dart';
import '../index.dart';

/// 视频列表元素
class VideoItem extends FisView<PastVideosController> {
  const VideoItem(
      {required this.index, required this.info, required this.onTap, Key? key})
      : super(key: key);
  final int index;
  final CourseInfoDetailDTO info;
  final VoidCallback onTap;

  static const double _cardInnerHeight = 80;
  @override
  FWidget build(BuildContext context) {
    return FInkWell(
      onTap: onTap,
      child: FContainer(
        margin: EdgeInsets.all(5),
        decoration: BoxDecoration(
          color: Colors.grey[200],
          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: FRow(
          children: [
            FExpanded(
              child: FRow(
                children: [
                  _buildVideoCoverImage(info.cover ?? ''),
                  FExpanded(
                    child: _buildVideoInfo(
                      info.name ?? '',
                      _durationToTime(info.duration),
                      info.createTime!.toLocal().toString().substring(0, 11),
                      info.sort,
                      info.isStick,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  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,
        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(
      height: _cardInnerHeight,
      margin: EdgeInsets.all(5),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: imageUrl.isEmpty ? Colors.blue : Colors.transparent,
      ),
      clipBehavior: Clip.antiAlias,
      child: QuickFWidget(
        AspectRatio(
          aspectRatio: 4 / 3,
          child: image,
        ),
      ),
    );
  }

  /// 构建视频信息
  FWidget _buildVideoInfo(
      String name, String duration, String date, int level, bool pinned) {
    return FContainer(
      height: _cardInnerHeight,
      margin: EdgeInsets.all(5),
      child: FColumn(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FRow(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              FExpanded(child: _buildVideoName(name)),
              // TODO 暂时屏蔽置顶和星级标签（需求来自群聊）
              // if (pinned) _buildStickyLabel(),
            ],
          ),
          FExpanded(
            child: FSizedBox(),
          ),
          FRow(
            children: [
              // TODO 暂时屏蔽置顶和星级标签（需求来自群聊）
              // _buildStarLevelLabel(level),
              FExpanded(
                child: FSizedBox(),
              ),
              // if (pinned) _buildStickyLabel(),
            ],
          ),
          FRow(
            children: [
              _buildVideoDuration(duration),
              FExpanded(
                child: FSizedBox(),
              ),
              _buildCreateDate(date),
            ],
          ),
        ],
      ),
    );
  }

  FWidget _buildVideoName(String name) {
    return FContainer(
      child: FText(
        name,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.bold,
          color: Color.fromARGB(255, 77, 77, 77),
        ),
      ),
    );
  }

  FWidget _buildVideoDuration(String duration) {
    return 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),
            ),
          ),
        ),
      ],
    );
  }

  FWidget _buildCreateDate(String date) {
    return FText(
      date,
      style: TextStyle(
        fontSize: 12,
        fontWeight: FontWeight.bold,
        color: Color.fromARGB(255, 77, 77, 77),
      ),
    );
  }

  // 构建视频星级标签
  FWidget _buildStarLevelLabel(int starLevel) {
    if (starLevel <= 0) {
      return FContainer();
    }
    return FContainer(
      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: 14,
            ),
          ],
        ],
      ),
    );
  }

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

  /// 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')}";
  }
}
