// ignore_for_file: deprecated_member_use

import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/views/podcasts/studio_details/studio_details_state.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class DetailsTabBar extends StatelessWidget {
  final bool isVideo;

  const DetailsTabBar({
    Key? key,
    required this.isVideo,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final state = context.watch<StudioDetailsState>();
    return WillPopScope(
      onWillPop: () async {
        state.selectedDetailsIndex = 0;
        return true;
      },
      child: Container(
        height: 72,
        decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.surface,
          boxShadow: [
            BoxShadow(
              color: const Color(0XFF1B3C59).withOpacity(0.15),
              offset: const Offset(0, 8),
              blurRadius: 8,
              spreadRadius: 0,
            )
          ],
        ),
        child: Row(
          children: [
            _TabItem(
              icon: DidvanIcons.description_solid,
              title: 'توضیحات',
              onTap: () => state.selectedDetailsIndex = 0,
              isSelected: state.selectedDetailsIndex == 0,
              isVideo: isVideo,
            ),
            _TabItem(
              icon: DidvanIcons.chats_solid,
              title: 'نظرات',
              onTap: () {
                state.selectedDetailsIndex = 1;
              },
              isSelected: state.selectedDetailsIndex == 1,
              isVideo: isVideo,
            ),
            _TabItem(
              icon: DidvanIcons.puzzle_solid,
              title: 'مطالب مرتبط',
              onTap: () => state.selectedDetailsIndex = 2,
              isSelected: state.selectedDetailsIndex == 2,
              isVideo: isVideo,
            ),
          ],
        ),
      ),
    );
  }
}

class _TabItem extends StatelessWidget {
  final IconData icon;
  final String title;
  final VoidCallback onTap;
  final bool isSelected;
  final bool isVideo;
  const _TabItem({
    Key? key,
    required this.icon,
    required this.title,
    required this.onTap,
    required this.isSelected,
    required this.isVideo,
  }) : super(key: key);

  Color? _color(context) {
    if (isSelected) {
      if (isVideo) {
        return Theme.of(context).colorScheme.secondary;
      }
      return Theme.of(context).colorScheme.focusedBorder;
    }
    return Theme.of(context).colorScheme.hint;
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: GestureDetector(
        onTap: onTap,
        child: Container(
          color: Colors.transparent,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                icon,
                color: _color(context),
              ),
              AnimatedContainer(
                duration: DesignConfig.lowAnimationDuration,
                width: isSelected ? 64 : 0,
                height: 1,
                color: _color(context),
              ),
              DidvanText(
                title,
                color: _color(context),
                style: Theme.of(context).textTheme.bodySmall,
              )
            ],
          ),
        ),
      ),
    );
  }
}
