import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:prosappco/src/components/column_padding.dart';

const double photoSize = 150;

class ReferenceBannerPhoto extends StatelessWidget {
  Reference? ref;
  double size;
  double sizeCircle;

  ReferenceBannerPhoto({
    super.key,
    required this.ref,
    this.size = photoSize,
    this.sizeCircle = photoSize,
  });

  Future<Widget> downloadImage() async {
    try {
      if (ref != null) {
        final imageData = await ref!.getData();
        if (imageData != null) {
          return Image.memory(
            imageData,
            width: double.infinity,
            height: size,
            fit: BoxFit.fill,
          );
        }
      }
      // ignore: empty_catches
    } catch (e) {}

    return DefaultPhoto(
      sizeDefault: sizeCircle,
    );
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Widget>(
        future: downloadImage(),
        builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            // mientras la llamada asíncrona está en proceso, muestra un mensaje de carga
            return DefaultPhoto();
          } else if (snapshot.connectionState == ConnectionState.done &&
              snapshot.hasData) {
            return snapshot.data!;
          } else {
            return DefaultPhoto();
          }
        });
  }
}

class DefaultPhoto extends StatelessWidget {
  double sizeDefault;
  DefaultPhoto({
    super.key,
    this.sizeDefault = photoSize,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.15),
            blurRadius: 5,
            offset: const Offset(0, 1),
          ),
        ],
      ),
      height: 150,
      child: ColumnPadding(
        alineacion: MainAxisAlignment.spaceAround,
        padding: const EdgeInsets.symmetric(horizontal: 70),
        children: [
          Container(
            decoration: BoxDecoration(
              color: const Color(0xFFD6F4FF),
              borderRadius: BorderRadius.circular(50),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.3),
                  spreadRadius: 2,
                  blurRadius: 5,
                  offset: const Offset(0, 3), // changes position of shadow
                ),
              ],
            ),
            constraints: const BoxConstraints(minWidth: 250, minHeight: 50),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: const [
                Expanded(
                  child: Center(
                    child: Text(
                      'Foto portada',
                      style: TextStyle(
                        color: Color(0xFF2BA4EC),
                        fontSize: 17,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(right: 15),
                  child: Icon(
                    Icons.file_upload_outlined,
                    color: Color(0xFF2BA4EC),
                    size: 30,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class LocalPhoto extends StatelessWidget {
  File file;
  LocalPhoto({super.key, required this.file});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.15),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Image.file(
        file,
        width: double.infinity,
        height: photoSize,
        fit: BoxFit.fill,
      ),
    );
  }
}
