import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

class RoutineViewCard extends StatefulWidget with HelperResponsiveLayout {
  const RoutineViewCard({
    super.key,
    required this.onTap,
    this.sceneOnTap,
    required this.icon,
    required this.textString,
    required this.spaceName,
    required this.cardType,
    this.isFromScenes,
    this.iconInBytes,
    required this.sceneId,
    required this.communityId,
    required this.spaceId,
    required this.automationId,
    required this.status,
    this.onChanged,
    required this.isLoading,
  });

  final Function() onTap;
  final Function()? sceneOnTap;

  final dynamic icon;
  final String textString;
  final String spaceName;
  final String cardType;
  final String sceneId;
  final String spaceId;
  final String status;
  final bool isLoading;

  final void Function(bool)? onChanged;
  final String automationId;
  final String communityId;

  final bool? isFromScenes;
  final Uint8List? iconInBytes;

  @override
  State<RoutineViewCard> createState() => _RoutineViewCardState();
}

class _RoutineViewCardState extends State<RoutineViewCard> {
  bool _showTemporaryCheck = false;

  void _handleSceneTap() {
    if (!_showTemporaryCheck) {
      setState(() => _showTemporaryCheck = true);
      widget.sceneOnTap?.call();
      Timer(const Duration(seconds: 3), () {
        if (mounted) setState(() => _showTemporaryCheck = false);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final double cardWidth = widget.isSmallScreenSize(context)
        ? 120
        : widget.isMediumScreenSize(context)
            ? 135
            : 150;

    final double cardHeight = widget.isSmallScreenSize(context) ? 190 : 200;

    final double iconSize = widget.isSmallScreenSize(context)
        ? 70
        : widget.isMediumScreenSize(context)
            ? 80
            : 90;

    return ConstrainedBox(
      constraints: BoxConstraints(
        maxWidth: cardWidth,
        maxHeight: cardHeight,
      ),
      child: Card(
        elevation: 3,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        color: ColorsManager.whiteColors,
        child: InkWell(
          borderRadius: BorderRadius.circular(10),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                widget.cardType != ''
                    ? Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          if (widget.isFromScenes ?? false)
                            InkWell(
                              onTap: _handleSceneTap,
                              child: Image.asset(
                                _showTemporaryCheck
                                    ? Assets.scenesPlayIcon
                                    : Assets.scenesPlayIconCheck,
                                fit: BoxFit.contain,
                              ),
                            )
                          else if (widget.isLoading)
                            const SizedBox(
                              width: 49,
                              height: 20,
                              child: Center(
                                child: SizedBox(
                                  width: 16,
                                  height: 16,
                                  child:
                                      CircularProgressIndicator(strokeWidth: 2),
                                ),
                              ),
                            )
                          else
                            CupertinoSwitch(
                              activeTrackColor: ColorsManager.primaryColor,
                              value: widget.status == 'enable',
                              onChanged: widget.onChanged,
                            )
                        ],
                      )
                    : const SizedBox(),
                Column(
                  children: [
                    Center(
                      child: InkWell(
                        customBorder: const CircleBorder(),
                        onTap: widget.onTap,
                        child: Container(
                          decoration: BoxDecoration(
                            color: ColorsManager.graysColor,
                            shape: BoxShape.circle,
                            border: Border.all(
                              color: ColorsManager.greyColor,
                              width: 2.0,
                            ),
                          ),
                          height: iconSize,
                          width: iconSize,
                          child: (widget.isFromScenes ?? false)
                              ? (widget.iconInBytes != null &&
                                      widget.iconInBytes?.isNotEmpty == true)
                                  ? Image.memory(
                                      widget.iconInBytes!,
                                      height: iconSize,
                                      width: iconSize,
                                      fit: BoxFit.contain,
                                      errorBuilder:
                                          (context, error, stackTrace) =>
                                              Image.asset(
                                        Assets.logo,
                                        height: iconSize,
                                        width: iconSize,
                                        fit: BoxFit.contain,
                                      ),
                                    )
                                  : Image.asset(
                                      Assets.logo,
                                      height: iconSize,
                                      width: iconSize,
                                      fit: BoxFit.contain,
                                    )
                              : (widget.icon is String &&
                                      widget.icon.endsWith('.svg'))
                                  ? SvgPicture.asset(
                                      height: iconSize,
                                      width: iconSize,
                                      widget.icon,
                                      fit: BoxFit.contain,
                                    )
                                  : Icon(
                                      widget.icon,
                                      color: ColorsManager.dialogBlueTitle,
                                      size: widget.isSmallScreenSize(context)
                                          ? 30
                                          : 40,
                                    ),
                        ),
                      ),
                    ),
                    const SizedBox(height: 8),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 3),
                      child: Column(
                        children: [
                          Text(
                            widget.textString,
                            textAlign: TextAlign.center,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                            style: context.textTheme.bodySmall?.copyWith(
                              color: ColorsManager.blackColor,
                              fontSize:
                                  widget.isSmallScreenSize(context) ? 10 : 12,
                            ),
                          ),
                          if (widget.spaceName != '')
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                SvgPicture.asset(
                                  Assets.spaceLocationIcon,
                                  fit: BoxFit.contain,
                                ),
                                Text(
                                  widget.spaceName,
                                  textAlign: TextAlign.center,
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 1,
                                  style: context.textTheme.bodySmall?.copyWith(
                                    color: ColorsManager.blackColor,
                                    fontSize: widget.isSmallScreenSize(context)
                                        ? 10
                                        : 12,
                                  ),
                                ),
                              ],
                            ),
                        ],
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
