import 'dart:math';

import 'package:flutter/material.dart';

class WaitingClock extends StatefulWidget {
  final Color outerRingColor;
  final Color innerRingColor;
  final Color handColor;
  final bool smoothAnimation;
  final double size;
  final double strokeWidth;

  const WaitingClock({
    Key? key,
    this.outerRingColor = const Color(0xFF8E44AD), // Пурпурный цвет как на изображении
    this.innerRingColor = Colors.white,
    this.handColor = const Color(0xFFF1C40F), // Желтый цвет как на изображении
    this.smoothAnimation = true, // По умолчанию плавная анимация
    this.size = 100.0,
    this.strokeWidth = 10.0,
  }) : super(key: key);

  @override
  State<WaitingClock> createState() => _WaitingClockState();
}

class _WaitingClockState extends State<WaitingClock> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this, duration: const Duration(seconds: 2))..repeat();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _controller,
      builder: (context, child) {
        return CustomPaint(
          size: Size(widget.size, widget.size),
          painter: ClockPainter(
            progress: widget.smoothAnimation ? _controller.value : (_controller.value * 12).floor() / 12, // Для рывков делим на 12 шагов
            outerRingColor: widget.outerRingColor,
            innerRingColor: widget.innerRingColor,
            handColor: widget.handColor,
            strokeWidth: widget.strokeWidth,
          ),
        );
      },
    );
  }
}

class ClockPainter extends CustomPainter {
  final double progress;
  final Color outerRingColor;
  final Color innerRingColor;
  final Color handColor;
  final double strokeWidth;

  ClockPainter({required this.progress, required this.outerRingColor, required this.innerRingColor, required this.handColor, required this.strokeWidth});

  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = min(size.width, size.height) / 2;

    // Внешнее кольцо (пурпурное)
    final outerRingPaint =
        Paint()
          ..color = outerRingColor
          ..style = PaintingStyle.stroke
          ..strokeWidth = strokeWidth;

    canvas.drawCircle(center, radius - strokeWidth / 2, outerRingPaint);

    // Draw the circular background
    final backgroundPaint =
        Paint()
          ..color = Color(0xFFDAD5D2)
          ..style = PaintingStyle.fill;

    canvas.drawCircle(center, radius, backgroundPaint);

    final xRate = 5;

    // Внутреннее кольцо (белое)
    final innerRingPaint =
        Paint()
          ..color = innerRingColor
          ..style = PaintingStyle.stroke
          ..strokeWidth = strokeWidth / 10;

    canvas.drawCircle(center, radius - strokeWidth / 2, innerRingPaint);

    // Рисуем метки минут (как на изображении полосы)
    final minuteMarkPaint =
        Paint()
          ..color = Colors.white
          ..style = PaintingStyle.stroke
          ..strokeWidth = 1;

    for (int i = 0; i < 60; i++) {
      final angle = (i * pi / 30);
      final markLength = i % 5 == 0 ? strokeWidth : strokeWidth / 2;

      final innerPoint = Offset(center.dx + (radius - strokeWidth * xRate) * cos(angle), center.dy + (radius - strokeWidth * xRate) * sin(angle));
      final outerPoint = Offset(center.dx + (radius - strokeWidth * xRate + markLength) * cos(angle), center.dy + (radius - strokeWidth * xRate + markLength) * sin(angle));

      canvas.drawLine(innerPoint, outerPoint, minuteMarkPaint);
    }

    // Стрелка
    final handAngle = 2 * pi * progress - pi / 2; // Начало с 12 часов

    final handPaint =
        Paint()
          ..color = handColor
          ..style = PaintingStyle.stroke
          ..strokeCap = StrokeCap.round
          ..strokeWidth = strokeWidth / 4;

    final handStart = center;

    final handEnd = Offset(center.dx + (radius - strokeWidth * 4.5) * cos(handAngle), center.dy + (radius - strokeWidth * 4.5) * sin(handAngle));

    canvas.drawLine(handStart, handEnd, handPaint);
  }

  @override
  bool shouldRepaint(covariant ClockPainter oldDelegate) {
    return oldDelegate.progress != progress ||
        oldDelegate.outerRingColor != outerRingColor ||
        oldDelegate.innerRingColor != innerRingColor ||
        oldDelegate.handColor != handColor ||
        oldDelegate.strokeWidth != strokeWidth;
  }
}

how to add shadow to clock hand to make more 3d look