import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'dart:math' as math;

class Cubo extends StatefulWidget {
  final double largo;
  final double ancho;
  final double alto;
  final String unidad;

  const Cubo({
    Key? key,
    required this.largo,
    required this.ancho,
    required this.alto,
    required this.unidad,
  }) : super(key: key);

  @override
  State<Cubo> createState() => _CuboState();
}

class _CuboState extends State<Cubo> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        SizedBox(
          width: 256.0,
          height: 192.0,
          child: CustomPaint(
            painter: _Cubo(),
          ),
        ),
        Positioned(
          child: Transform.rotate(
            child: Container(
              width: 256.0 / 2,
              padding: const EdgeInsets.all(4.0),
              child: Text(
                "l: ${widget.largo} ${widget.unidad}",
                textAlign: TextAlign.center,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: GoogleFonts.roboto(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 18.0,
                ),
              ),
            ),
            angle: _degreeToRadian(14),
          ),
          left: 0.0,
          bottom: 18.0,
        ),
        Positioned(
          child: Transform.rotate(
            child: Container(
              width: 256.0 / 2,
              padding: const EdgeInsets.all(4.0),
              child: Text(
                "a: ${widget.ancho} ${widget.unidad}",
                textAlign: TextAlign.center,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: GoogleFonts.roboto(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 18.0,
                ),
              ),
            ),
            angle: _degreeToRadian(-14),
          ),
          right: 0.0,
          bottom: 18.0,
        ),
        Positioned(
          child: Transform.rotate(
            child: Container(
              width: 192.0 - 64.0,
              padding: const EdgeInsets.all(4.0),
              child: Text(
                "h: ${widget.alto} ${widget.unidad}",
                textAlign: TextAlign.center,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: GoogleFonts.roboto(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 18.0,
                ),
              ),
            ),
            angle: _degreeToRadian(-90),
          ),
          left: 46.0,
          bottom: 64.0,
        )
      ],
    );
  }

  double _degreeToRadian(double degree){
    return degree * math.pi / 180;
  }
}

class _Cubo extends CustomPainter{
  @override
  void paint(Canvas canvas, Size size) {
    final borderPaint = Paint();
    borderPaint.color = Colors.black;
    borderPaint.style = PaintingStyle.stroke;
    borderPaint.strokeWidth = 2.5;

    final borderPath = Path();
    final fillPath = Path();

    final x = size.width;
    final y = size.height;

    final fillPaint = Paint();
    fillPaint.shader = const RadialGradient(
      colors: [
        Color(0xFFE57373),
        Color(0xFFB40404),
      ],
    ).createShader(Rect.fromCircle(
      center: Offset(x / 2, 64.0),
      radius: 256.0,
    ));
    fillPaint.style = PaintingStyle.fill;

    borderPath.moveTo(x / 2, y);
    borderPath.lineTo(0, y - 32.0);
    borderPath.lineTo(0, 32.0);
    borderPath.lineTo(x / 2, 64.0);
    borderPath.lineTo(x / 2, y);

    fillPath.moveTo(x / 2, y);
    fillPath.lineTo(0, y - 32.0);
    fillPath.lineTo(0, 32.0);
    fillPath.lineTo(x / 2, 64.0);
    fillPath.lineTo(x / 2, y);

    borderPath.lineTo(x, y - 32.0);
    borderPath.lineTo(x, 32.0);
    borderPath.lineTo(x / 2, 64.0);

    fillPath.lineTo(x, y - 32.0);
    fillPath.lineTo(x, 32.0);
    fillPath.lineTo(x / 2, 64.0);

    borderPath.moveTo(0, 32.0);
    borderPath.lineTo(x / 2, 0);
    borderPath.lineTo(x, 32.0);

    fillPath.moveTo(0, 32.0);
    fillPath.lineTo(x / 2, 0);
    fillPath.lineTo(x, 32.0);
    fillPath.lineTo(x / 2, 64.0);

    canvas.drawPath(fillPath, fillPaint);
    canvas.drawPath(borderPath, borderPaint);
  }
  
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
