import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:remever/common/resources.dart';
import 'package:remever/common/widgets/typography.dart';
import 'package:remever/common/widgets/w_if.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/database/database.dart';
import 'package:remever/widgets/primary_button.dart';

class InfoDialog extends StatelessWidget {
  const InfoDialog({super.key, required this.collection});

  final Collection collection;

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        decoration: const BoxDecoration(color: Colors.white),
        constraints: BoxConstraints(
          minHeight: MediaQuery.sizeOf(context).height / 6,
          maxHeight: MediaQuery.sizeOf(context).height / 1.1,
        ),
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 32, horizontal: 16).r,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Row(
                children: <Widget>[
                  _buildAvatar(),
                  const WSpace(5),
                  Flexible(child: _buildCollectionTitle()),
                ],
              ),
              const HSpace(8),
              _buildText(),
              const HSpace(16),
              PrimaryButton(
                onTap: () => Navigator.of(context).pop(),
                color: AppColors.primary,
                child: AppTypography(
                  'Закрыть',
                  type: Medium14px(),
                  color: AppColors.white,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildText() {
    return Flexible(
      child: SingleChildScrollView(
        controller: ScrollController(),
        child: AppTypography(
          collection.desc,
          type: Regular14px(),
          maxLines: 9999,
        ),
      ),
    );
  }

  ///
  /// Название коллекции
  ///
  Widget _buildCollectionTitle() {
    return AppTypography(
      collection.title,
      type: Medium16px(),
      maxLines: 2,
      softWrap: true,
      color: AppColors.primary,
    );
  }

  ///
  /// Обложка коллекции
  ///
  Widget _buildAvatar() {
    return SizedBox.square(
      dimension: 40.r,
      child: DecoratedBox(
        decoration: BoxDecoration(shape: BoxShape.circle, color: AppColors.bg),

        child: Wif(
          condition: collection.image != null,
          builder:
              (context) => ClipOval(
                child: Image.memory(collection.image!, fit: BoxFit.cover),
              ),
          fallback:
              (context) => Center(
                child: AppTypography(
                  collection.title.substring(0, 1),
                  type: Bold34px(),
                ),
              ),
        ),
      ),
    );
  }
}
