/* 
 * This file is part of Stack Wallet.
 * 
 * Copyright (c) 2023 Cypher Stack
 * All Rights Reserved.
 * The code is distributed under GPLv3 license, see LICENSE file for details.
 * Generated by Cypher Stack on 2023-05-26
 *
 */

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import '../../themes/stack_colors.dart';
import '../../themes/theme_providers.dart';
import '../../utilities/assets.dart';

class FavoriteToggle extends ConsumerStatefulWidget {
  const FavoriteToggle({
    super.key,
    this.backGround,
    this.borderRadius = BorderRadius.zero,
    this.initialState = false,
    this.on,
    this.off,
    required this.onChanged,
  });

  final Color? backGround;
  final Color? on;
  final Color? off;
  final BorderRadiusGeometry borderRadius;
  final bool initialState;
  final void Function(bool)? onChanged;

  @override
  ConsumerState<FavoriteToggle> createState() => _FavoriteToggleState();
}

class _FavoriteToggleState extends ConsumerState<FavoriteToggle> {
  late bool _isActive;
  late Color _color;
  late void Function(bool)? _onChanged;

  late final Color on;
  late final Color off;

  @override
  void initState() {
    on = widget.on ?? ref.read(themeProvider.state).state.favoriteStarActive;
    off =
        widget.off ?? ref.read(themeProvider.state).state.favoriteStarInactive;
    _isActive = widget.initialState;
    _color = _isActive ? on : off;
    _onChanged = widget.onChanged;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: widget.backGround,
        borderRadius: widget.borderRadius,
      ),
      child: MaterialButton(
        splashColor: Theme.of(context).extension<StackColors>()!.highlight,
        minWidth: 0,
        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
        shape: RoundedRectangleBorder(
          borderRadius: widget.borderRadius,
        ),
        onPressed: _onChanged != null
            ? () {
                _isActive = !_isActive;
                setState(() {
                  _color = _isActive ? on : off;
                });
                _onChanged!.call(_isActive);
              }
            : null,
        child: SvgPicture.asset(
          Assets.svg.star,
          width: 16,
          height: 16,
          color: _color,
        ),
      ),
    );
  }
}