import 'dart:math';

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:remever/common/resources.dart';
import 'package:remever/common/widgets/typography.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/gen/assets.gen.dart';
import 'package:remever/router.gr.dart';
import 'package:remever/screens/collections/widgets/collection_progress_bar.dart';
import 'package:remever/screens/dialogs/dialog_header.dart';

class ReplaceDialog extends StatelessWidget {
  const ReplaceDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        const DialogHeader(title: 'Переместить карточку'),
        const HSpace(16),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 28).r,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              AppTypography('из коллекции', type: Medium16px()),
              const HSpace(8),
              _buildCollection(context),
              const HSpace(16),
              Center(
                child: SizedBox.square(
                  dimension: 34.r,
                  child: DecoratedBox(
                    decoration: const BoxDecoration(
                      shape: BoxShape.circle,
                      color: AppColors.gray,
                    ),
                    child: Center(
                      child: Assets.icons.typeArrowDown.image(
                        height: 18.h,
                        width: 18.w,
                      ),
                    ),
                  ),
                ),
              ),
              const HSpace(16),
              AppTypography('в коллекцию', type: Medium16px()),
              const HSpace(8),
              _buildCollection(context),
              const HSpace(16),
              _createBtn(context),
            ],
          ),
        ),
      ],
    );
  }

  Widget _createBtn(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
        // context.read<HomeCubit>().toCrudCollection(CrudType.CREATE);
        context.pushRoute(CrudCollectionRoute());
      },
      child: Row(
        children: <Widget>[
          SizedBox.square(
            dimension: 20.r,
            child: DecoratedBox(
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
                color: AppColors.gray,
              ),
              child: Assets.icons.typePlus.image(color: Colors.black54),
            ),
          ),
          const WSpace(4),
          AppTypography('Создать и перенести в новую', type: Regular16px()),
        ],
      ),
    );
  }

  Widget _buildCollection(BuildContext context) {
    return GestureDetector(
      onTap: () {},
      child: Container(
        constraints: BoxConstraints(minHeight: 66.h, maxHeight: 84.h),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12).r,
          color: AppColors.white,
        ),
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8).r,
        child: Row(
          children: <Widget>[_buildAvatar(), const WSpace(5), _buildInfo()],
        ),
      ),
    );
  }

  ///
  /// Построение основной информации
  ///
  Widget _buildInfo() {
    return SizedBox(
      width: 230.w,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          _buildTitle(),
          const HSpace(4),
          Row(
            children: <Widget>[
              Assets.icons.typeCards.image(
                height: 18.h,
                width: 18.w,
                color: AppColors.disabled,
              ),
              const WSpace(2),
              AppTypography(
                Random().nextInt(654).toString(),
                type: Regular14px(),
                color: AppColors.disabled,
              ),
            ],
          ),
          const HSpace(6),
          const CollectionProgressBar(),
        ],
      ),
    );
  }

  ///
  /// Название коллекции
  ///
  Widget _buildTitle() {
    return AppTypography(
      'Астрономия',
      type: Medium16px(),
      maxLines: 2,
      softWrap: true,
    );
  }

  ///
  /// Обложка коллекции
  ///
  Widget _buildAvatar() {
    return SizedBox.square(
      dimension: 50.r,
      child: DecoratedBox(
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          image: DecorationImage(image: Assets.images.img.provider()),
        ),
      ),
    );
  }
}
