import 'package:agrilink_vocpro/core/constant/app_color.dart';
import 'package:agrilink_vocpro/core/constant/app_theme.dart';
import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/features/control/provider/control_provider.dart';
import 'package:bootstrap_icons/bootstrap_icons.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class ControlButtonWidget extends StatelessWidget {
  const ControlButtonWidget({
    super.key,
    required this.title,
    required this.subtitle,
    required this.isActive,
    required this.onTap,
  });

  final String title;
  final String subtitle;
  final bool isActive;
  final Function() onTap;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(16.r),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16.r),
        boxShadow: [
          BoxShadow(
            color: isActive
                ? AppColor.secondary.withOpacity(0.2)
                : Colors.grey.withOpacity(0.2),
            spreadRadius: 1.r,
            blurRadius: 16.r,
            offset: Offset(0, 12.r),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(title, style: AppTheme.labelMedium),
          Text(subtitle, style: AppTheme.labelSmall),
          const Spacer(),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Consumer<ControlProvider>(
                builder: (context, provider, child) {
                  switch (provider.relayState) {
                    case ResultState.loading:
                      return CircleAvatar(
                        radius: 20.r,
                        backgroundColor: Colors.transparent,
                        child: const CupertinoActivityIndicator(),
                      );
                    default:
                      return InkWell(
                        highlightColor: Colors.black,
                        onTap: onTap,
                        child: CircleAvatar(
                          radius: 20.r,
                          backgroundColor: isActive
                              ? AppColor.secondary
                              : Colors.grey.shade400,
                          child: const Icon(
                            BootstrapIcons.power,
                            color: Colors.white,
                          ),
                        ),
                      );
                  }
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}
