// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:wyatt_ui_components/wyatt_ui_components.dart';
import 'package:wyatt_ui_kit/wyatt_ui_kit.dart';
import 'package:wyatt_ui_kit_example/theme/constants.dart';

class FileSelectionButtons extends StatefulWidget {
  const FileSelectionButtons({super.key});

  @override
  State<FileSelectionButtons> createState() => _FileSelectionButtonsState();
}

class _FileSelectionButtonsState extends State<FileSelectionButtons> {
  final _disabled = ValueNotifier(true);
  final _dynamic = ValueNotifier(false);

  Widget _leading() => const DecoratedBox(
        decoration: BoxDecoration(
          color: Constants.grey2,
          borderRadius: BorderRadius.all(
            Radius.circular(5),
          ),
        ),
        child: Padding(
          padding: EdgeInsets.all(8),
          child: Icon(
            Icons.file_upload_outlined,
            color: Colors.white,
          ),
        ),
      );

  @override
  Widget build(BuildContext context) => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'FileSelection Buttons',
            style: Theme.of(context).textTheme.titleMedium,
          ),
          const Gap(20),
          // Default
          FileSelectionButton(
            leading: _leading(),
            title: const TextWrapper(
              'Ajouter un fichier',
            ),
            subTitle: const TextWrapper('Taille max: 20 Mo'),
          ),
          const Gap(20),
          BlocProvider(
            create: (context) => InvalidButtonCubit()..freeze(),
            child: FileSelectionButton(
              leading: _leading(),
              title: const TextWrapper(
                'Enabled',
              ),
              subTitle: const TextWrapper('Subtitle'),
            ),
          ),
          const Gap(20),
          // Using a ValueNotifier to disable the button
          FileSelectionButton(
            leading: _leading(),
            title: const TextWrapper(
              'Disabled',
            ),
            subTitle: const TextWrapper('Subtitle'),
            disabled: _disabled,
          ),
          const Gap(20),
          BlocProvider(
            create: (context) => InvalidButtonCubit()..hover(),
            child: FileSelectionButton(
              leading: _leading(),
              title: const TextWrapper(
                'Hovered',
              ),
              subTitle: const TextWrapper('Subtitle'),
            ),
          ),
          const Gap(20),
          BlocProvider(
            create: (context) => InvalidButtonCubit()..focus(),
            child: FileSelectionButton(
              leading: _leading(),
              title: const TextWrapper(
                'Focused',
              ),
              subTitle: const TextWrapper('Subtitle'),
            ),
          ),
          const Gap(20),
          BlocProvider(
            create: (context) => InvalidButtonCubit()..tap(),
            child: FileSelectionButton(
              leading: _leading(),
              title: const TextWrapper(
                'Tapped',
              ),
              subTitle: const TextWrapper('Subtitle'),
            ),
          ),
          const Gap(20),
          BlocProvider(
            create: (context) => InvalidButtonCubit()..invalidate(),
            child: FileSelectionButton(
              leading: _leading(),
              title: const TextWrapper(
                'Invalid',
              ),
              subTitle: const TextWrapper('Subtitle'),
            ),
          ),
          const Gap(20),
          // Dynamic, disabled with ValueNotifier and invalid with BlocProvider.
          // Keep the state when the button is disabled.
          Column(
            children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text('Enabled'),
                  Checkbox(
                    value: !_dynamic.value,
                    onChanged: (value) {
                      setState(() {
                        _dynamic.value = !_dynamic.value;
                      });
                    },
                  ),
                ],
              ),
              FileSelectionButton(
                leading: _leading(),
                title: const TextWrapper(
                  'Dynamic',
                ),
                subTitle: const TextWrapper('Subtitle'),
                disabled: _dynamic,
              ),
            ],
          ),
          const Gap(20),
        ],
      );
}
