import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_image_animated_placeholder.dart';

import '../styles/theme.dart';

class ProfileIcon extends StatefulWidget {
  const ProfileIcon({
    super.key,
    required this.onChange,
    this.diameter = 64,
    this.imagePath,
    this.imageUrl,
    this.imageQuality = 80,
  });

  final double diameter;
  final String? imagePath;
  final String? imageUrl;
  final int imageQuality;
  final Function(String) onChange;

  @override
  State<ProfileIcon> createState() => _ProfileIconState();
}

class _ProfileIconState extends State<ProfileIcon> {
  String? _imagePath;
  String? _imageUrl;
  final ImagePicker _picker = ImagePicker();

  Future<void> _pickImage([ImageSource source = ImageSource.gallery]) async {
    final XFile? image = await _picker.pickImage(
      source: source,
      imageQuality: widget.imageQuality,
    );

    if (image != null) {
      setState(() => _imagePath = image.path);

      widget.onChange(image.path);
    }
  }

  @override
  void initState() {
    super.initState();

    _imagePath = widget.imagePath;
    _imageUrl = widget.imageUrl;
  }

  @override
  void didUpdateWidget(covariant ProfileIcon oldWidget) {
    _imagePath = widget.imagePath;
    _imageUrl = widget.imageUrl;
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    final isImageAvailable = _imagePath != null || widget.imageUrl != null;
    Widget avatar;

    if (_imagePath != null) {
      avatar = Image.file(
        File(_imagePath!),
        fit: BoxFit.cover,
        frameBuilder: (_, child, frame, __) {
          return frame != null ? child : const KwAnimatedImagePlaceholder();
        },
      );
    } else if (_imageUrl != null) {
      avatar = Image.network(
        _imageUrl!,
        fit: BoxFit.cover,
        frameBuilder: (_, child, frame, __) {
          return frame != null ? child : const KwAnimatedImagePlaceholder();
        },
      );
    } else {
      avatar = DecoratedBox(
        decoration: const BoxDecoration(
          color: AppColors.grayWhite,
        ),
        child: Assets.images.icons.person.svg(
          height: widget.diameter/2,
          width: widget.diameter/2,
          fit: BoxFit.scaleDown,
        ),
      );
    }

    return GestureDetector(
      onTap: _pickImage,
      child: Stack(
        clipBehavior: Clip.none,
        children: [
          ClipOval(
            child: SizedBox(
              height: widget.diameter,
              width: widget.diameter,
              child: avatar,
            ),
          ),
          PositionedDirectional(
            bottom: 0,
            end: -5,
            child: Container(
              height: widget.diameter/4+10,
              width: widget.diameter/4+10,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: isImageAvailable
                    ? AppColors.grayWhite
                    : AppColors.bgColorDark,
                shape: BoxShape.circle,
                border: Border.all(
                  color: AppColors.bgColorLight,
                  width: 2,
                ),
              ),
              child: isImageAvailable
                  ? Assets.images.icons.edit.svg(
                      width: widget.diameter/4,
                      height: widget.diameter/4,
                    )
                  : Assets.images.icons.add.svg(
                      width: widget.diameter/4,
                      height: widget.diameter/4,
                      fit: BoxFit.scaleDown,
                      colorFilter: const ColorFilter.mode(
                        AppColors.grayWhite,
                        BlendMode.srcIn,
                      ),
                    ),
            ),
          ),
        ],
      ),
    );
  }
}
