import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nut_player_example/src/features/player_screen/domain/bloc/playerview_bloc.dart';
import 'package:nut_player_example/src/features/player_screen/domain/custom_skin_bloc/custom_skin_bloc.dart';

class PlayerCustomSkin extends StatelessWidget {
  const PlayerCustomSkin({super.key});

  @override
  Widget build(BuildContext context) {
    final bloc = CustomSkinBloc(context.read<PlayerViewBloc>());

    return BlocProvider(
      create: (context) => bloc,
      child: GestureDetector(
        onTap: () { bloc.add(SkinVisibilityChanged()); },
        child: BlocBuilder<CustomSkinBloc, CustomSkinState>(
          builder: (context, state) {
            final skinState = state;
            if (skinState is! CustomSkin) { return const Text('Incorrect widget'); }
            return AbsorbPointer(
              absorbing: !skinState.isVisible,
              child: Visibility(
                maintainSize: true,
                maintainAnimation: true,
                maintainState: true,
                visible: skinState.isVisible,
                child: Container(
                    decoration: BoxDecoration(
                        gradient: RadialGradient(
                            radius: 2,
                            colors: [
                              CupertinoColors.white.withOpacity(0.1),
                              CupertinoColors.systemMint.withOpacity(1)
                            ]
                        )
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              CupertinoButton(
                                  onPressed: () {
                                    bloc.add(PlayerSeekBackEvent());
                                  },
                                  child: Image.asset('assets/skinIcons/rewind_back.png', width: 50)
                              ),

                              _buildPlaybackButton(bloc),

                              CupertinoButton(
                                  onPressed: () {
                                    bloc.add(PlayerSeekForwardEvent());
                                  },
                                  child: Image.asset('assets/skinIcons/rewind_forward.png', width: 50)
                              ),
                            ]
                        ),

                        if (skinState.duration >= skinState.currentPosition && skinState.duration > 0)
                          _buildTimeArea(bloc)
                      ],
                    )),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget _buildTimeArea(CustomSkinBloc skinBloc) {
    final skinState = skinBloc.state;
    if (skinState is! CustomSkin) { return const Text('Unsupported view'); }

    return Column(
      children: [
        Container(
          width: double.infinity,
          margin: const EdgeInsets.symmetric(horizontal: 10),
          child: CupertinoSlider(
              value: skinState.currentPosition,
              activeColor: CupertinoColors.systemMint,
              thumbColor: CupertinoColors.white,
              min: 0,
              max: skinState.duration,
              onChangeEnd: (value) {
                skinBloc.add(PlayerSeekEvent(value));
              },
              onChanged: (value) {
                skinBloc.add(CurrentPositionChanged(value.roundToDouble()));
              }),
        ),
        Container(
          margin: const EdgeInsets.symmetric(horizontal: 20),
          child: Row(
            children: [
              Text(_timeFormatter(skinState.currentPosition),
                  style: const TextStyle(
                      color: CupertinoColors.white,
                      fontSize: 12,
                      decoration: TextDecoration.none)),

              const Spacer(),

              Text(_timeFormatter(skinState.duration),
                  style: const TextStyle(
                      color: CupertinoColors.white,
                      fontSize: 12,
                      decoration: TextDecoration.none)),
            ],
          ),
        )
      ],
    );
  }

  Widget _buildPlaybackButton(CustomSkinBloc skinBloc) {
    final skinState = skinBloc.state;
    if (skinState is! CustomSkin) { return const Text('Incorrect widget'); }

    return CupertinoButton(
        onPressed: () {
          skinBloc.add(PlaybackChanged());
        },
        child: Image.asset(skinState.isPlaying ? 'assets/skinIcons/pause.png' : 'assets/skinIcons/play.png', width: 70)
    );
  }

  String _timeFormatter(double time) {
    Duration duration = Duration(seconds: time.round());

    return [duration.inHours, duration.inMinutes, duration.inSeconds]
        .map((seg) => seg.remainder(60).toString().padLeft(2, '0'))
        .join(':');
  }
}
