import 'package:flutter/material.dart';
import 'package:general_package/themes/themes.dart';

import '../../../constants/constants.dart';
import '../utils/constants.dart';
import '../utils/paddings.dart';
import '../model/onboard_model.dart';
import '../widgets/dots_selector.dart';
import '../widgets/onboard_body.dart';
import '../widgets/skip_button.dart';

class Onboard extends StatefulWidget {
  /// This color is generally used in the project.
  final Color? primaryColor;

  ///Required to create pages.
  ///Length of the list must be more than 2.
  final List<OnboardModel> onboardPages;
  final List<OnboardPermissionModel> permissionOnboardPages;

  ///This func works when skip button is pressed.
  final VoidCallback skipButtonPressed;

  ///This text will redirect to mentioned page
  final String nextScreen;

  ///This text will appear in the skip button.
  final String? skipText;

  ///This text will appear on the next button on all pages except the last page.
  final String? nextText;

  ///This text will appear in the next button on the last page.
  final String? lastText;

  ///Onboard Widget for use in your project.
  Onboard(
      {Key? key,
      this.primaryColor,
      required this.onboardPages,
      required this.permissionOnboardPages,
      required this.skipButtonPressed,
      required this.nextScreen,
      this.skipText,
      this.nextText,
      this.lastText})
      :
        // : assert(onboardPages.isNotEmpty),
        //   assert(onboardPages.length > 2),
        //   assert(nextText != null),
        //   assert(skipText != null),
        //   assert(lastText != null),
        super(key: key);

  @override
  _OnboardState createState() => _OnboardState();
}

class _OnboardState extends State<Onboard> {
  int _currentIndex = 0;
  final PageController _pageController = PageController();
  bool isOnBoardOpen = true;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: AppTheme.whiteColor,
        body: Column(
          children: [
            const Spacer(flex: 1),
            // Expanded(flex: 5, child: _topButtons(context)),
            // const Spacer(flex: 1),
            if (isOnBoardOpen) Expanded(flex: 40, child: _onBoardBodyWidget()),
            // if (isOnBoardOpen) const Spacer(flex: 3),
            if (isOnBoardOpen)
              Expanded(flex: 9, child: _bottomButtons(context)),
            if (!isOnBoardOpen)
              Expanded(flex: 50, child: _permissionOnBoardBodyWidget()),
          ],
        ),
      ),
    );
  }

  Widget _permissionOnBoardBodyWidget() {
    return Container(
      padding: GeneralConstants.instance.padding15,
      child: PageView.builder(
        onPageChanged: (value) {
          setState(() {
            _currentIndex = value;
          });
        },
        controller: _pageController,
        itemCount: widget.permissionOnboardPages.length,
        itemBuilder: (context, index) => PermissionOnboardBody(
            onboardPermissionModel: widget.permissionOnboardPages[index],
            removePermission: () {
              setState(() {
                widget.permissionOnboardPages
                    .remove(widget.permissionOnboardPages[index]);
              });
            },
            count: widget.permissionOnboardPages.length,
            nextScreen: widget.nextScreen),
      ),
    );
  }

  PageView _onBoardBodyWidget() {
    return PageView.builder(
      onPageChanged: (value) {
        setState(() {
          _currentIndex = value;
        });
      },
      controller: _pageController,
      itemCount: widget.onboardPages.length,
      itemBuilder: (context, index) => OnboardBody(
        onboardModel: widget.onboardPages[index],
      ),
    );
  }

  Widget _bottomButtons(BuildContext context) {
    return Container(
      padding: GeneralConstants.instance.padding15,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const Spacer(flex: 1),
          Expanded(
              flex: 10,
              child: DotSelector(
                primaryColor: widget.primaryColor,
                pageCount: widget.onboardPages.length,
                selectedPage: _currentIndex,
              )),
          const Spacer(flex: 30),
          TextButton(
            onPressed: () {
              setState(() {
                isOnBoardOpen = false;
              });
            },
            child: Text(
              (widget.onboardPages.length - 1 == _currentIndex)
                  ? widget.lastText ?? StringConstants.instance.lastPageText
                  : widget.skipText ?? StringConstants.instance.skipText,
              style: Theme.of(context).textTheme.button,
            ),
          )
          // OnboardSkipButton(
          //   title: (widget.onboardPages.length-1==_currentIndex)? widget.lastText?? StringGeneralConstants.instance.lastPageText: widget.skipText ?? StringGeneralConstants.instance.skipText,
          //   onPressed: widget.skipButtonPressed,
          // )
        ],
      ),
    );
  }
}
