import 'package:flutter/material.dart';

import '../../../core/contracts/library.dart';

class AndroidMediaInfoSection extends StatelessWidget {
  final EmbyRawMediaSource? source;
  final List<String> studioNames;
  final List<String> directors;
  final Widget? leading;

  const AndroidMediaInfoSection({
    super.key,
    this.source,
    this.studioNames = const [],
    this.directors = const [],
    this.leading,
  });

  @override
  Widget build(BuildContext context) {
    final rows = _buildRows();
    if (rows.isEmpty) return const SizedBox.shrink();

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (leading != null) leading!,
        Padding(
          padding: const EdgeInsets.fromLTRB(16, 0, 16, 0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: rows,
          ),
        ),
      ],
    );
  }

  List<Widget> _buildRows() {
    final src = source;
    final streams = src?.MediaStreams ?? [];
    final rows = <Widget>[];

    final subCount = streams.where((s) => s.Type == 'Subtitle').length;
    if (subCount > 0) {
      _addRow(rows, '字幕', '$subCount 条字幕轨');
    }

    if (directors.isNotEmpty) {
      _addRow(rows, '导演', directors.join(' · '));
    }

    if (studioNames.isNotEmpty) {
      _addRow(rows, '工作室', studioNames.join(' · '));
    }

    return rows;
  }

  void _addRow(List<Widget> rows, String label, String value) {
    if (rows.isNotEmpty) {
      rows.add(Divider(color: Colors.white.withValues(alpha: 0.04), height: 1));
    }
    rows.add(_InfoRow(label: label, value: value));
  }
}

class _InfoRow extends StatelessWidget {
  final String label;
  final String value;

  const _InfoRow({required this.label, required this.value});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        children: [
          SizedBox(
            width: 56,
            child: Text(
              label,
              style: TextStyle(
                fontSize: 13,
                color: Colors.white.withValues(alpha: 0.55),
              ),
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: TextStyle(
                fontSize: 13,
                color: Colors.white.withValues(alpha: 0.75),
              ),
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
    );
  }
}
