import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(const TindBook());

class TindBook extends StatelessWidget {
  const TindBook({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => TindBookState(),
      child: MaterialApp(
        title: 'Tind Book',
        home: Scaffold(
          appBar: AppBar(title: const Text('Draggable Sample')),
          body: const MyHomePage(),
        ),
      ),
    );
  }
}

class TindBookState extends ChangeNotifier {
  // List of image paths
  List<String> imagePaths = [
    'assets/images/passeur.png',
    'assets/images/1.jpg',
    'assets/images/2.jpg',
    // Add more image paths as needed
  ];

  // Index of the currently displayed image
  int currentImageIndex = 0;

  // Function to handle swiping in any direction
  void handleSwipe(int direction) {
    if (direction == 1) {
      // Swipe right
      if (currentImageIndex < imagePaths.length - 1) {
        currentImageIndex++;
        notifyListeners();
      }
    } else {
      // Swipe left
      if (currentImageIndex > 0) {
        currentImageIndex--;
        notifyListeners();
      }
    }
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    Widget page;
    switch (selectedIndex) {
      case 0:
        page = TindBookContent();
        break;
      case 1:
        page = FavoritePage();
        break;
      case 2:
        page = Placeholder();
        break;
      default:
        throw UnimplementedError('no widget for $selectedIndex');
    }

    return LayoutBuilder(
      builder: (context, constraints) {
        return Scaffold(
          body: Row(
            children: [
              SafeArea(
                child: NavigationRail(
                  extended: constraints.maxWidth >= 600,
                  destinations: [
                    NavigationRailDestination(
                      icon: Icon(Icons.home),
                      label: Text('Home'),
                    ),
                    NavigationRailDestination(
                      icon: Icon(Icons.swipe),
                      label: Text('Swipe'),
                    ),
                    NavigationRailDestination(
                      icon: Icon(Icons.info),
                      label: Text('About'),
                    ),
                  ],
                  selectedIndex: selectedIndex,
                  onDestinationSelected: (value) {
                    print('selected: $value');
                    setState(() {
                      selectedIndex = value;
                    });
                  },
                ),
              ),
              Expanded(
                child: Container(
                  color: Theme.of(context).colorScheme.primaryContainer,
                  child: page,
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}

class TindBookContent extends StatefulWidget {
  const TindBookContent({Key? key}) : super(key: key);

  @override
  State<TindBookContent> createState() => _TindBookContentState();
}

class _TindBookContentState extends State<TindBookContent> {
  late TindBookState state;
  double _startX = 0.0;
  double _currentX = 0.0;
  double _deltaX = 0.0;
  bool _isSwiping = false;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    state = Provider.of<TindBookState>(context);
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        double containerWidth = constraints.maxWidth;
        double containerHeight = constraints.maxHeight;

        return GestureDetector(
          onHorizontalDragStart: (details) {
            _startX = details.globalPosition.dx;
            _isSwiping = true;
          },
          onHorizontalDragUpdate: (details) {
            if (_isSwiping) {
              _currentX = details.globalPosition.dx;
              _deltaX = _currentX - _startX;
              setState(() {});
            }
          },
          onHorizontalDragEnd: (details) {
            if (_isSwiping) {
              _isSwiping = false;
              if (_deltaX > 50) {
                // Droite
                state.handleSwipe(1);
              } else if (_deltaX < -50) {
                //Gauche
                state.handleSwipe(-1);
              }
            }
            _startX = 0.0;
            _currentX = 0.0;
            _deltaX = 0.0;
          },
          child: Stack(
            children: [
              Positioned.fill(
                child: Container(
                  color: Colors.deepOrange,
                  height: containerHeight,
                  width: containerWidth,
                ),
              ),
              Positioned(
                top: 0,
                left: 0,
                right: 0,
                bottom: 0,
                child: Transform.translate(
                  offset: Offset(_deltaX, 0.0),
                  child: Transform.rotate(
                    angle: _deltaX * 0.0002, 
                    child: Image.asset(
                      state.imagePaths[state.currentImageIndex],
                      height: containerHeight,
                      width: containerWidth,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}

class FavoritePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Text('Test'),
        ],
      ),
    );
  }
}
