
import 'package:flutter/material.dart';
import '../../routes.dart';
import '../../store/store.dart';
import '../../events/eventbus.dart';
import '../../events/events.dart';
import '../../generated/l10n.dart';
import '../../models/user.dart';
import '../../utils/utils.dart';
import '../../widgets/general/text_link.dart';
import '../../widgets/mobile/mobile_navigation_drawer_header.dart';

import '../../constants.dart';

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

  @override
  State<StatefulWidget> createState() {
    return MobileNavigationDrawerState();
  }

}

class MobileNavigationDrawerState extends State<MobileNavigationDrawer> {
  User? _user;

  @override
  Widget build(BuildContext context) {
    String currentRoute = ModalRoute.of(context)?.settings.name ?? '';

    return Container(
      width: 200.0,
      height: MediaQuery.of(context).size.height,
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(color: Colors.black12, blurRadius: 16.0),
        ],
      ),
      child: SingleChildScrollView(
        child: Column(
          children: [
            MobileNavigationDrawerHeader(),
            TextLink(
              S.of(context).home,
              '/',
              paddingVertical: 10.0,
              paddingHorizontal: 15.0,
              selected: currentRoute == '/',
              closeDrawer: true,
              clearStack: true,
            ),
            TextLink(
              S.of(context).download,
              '/download',
              paddingVertical: 10.0,
              paddingHorizontal: 15.0,
              selected: currentRoute == '/download',
              closeDrawer: true,
            ),
            TextLink(
              S.of(context).tutorials,
              Constants.TUTORIAL_URL,
              paddingVertical: 10.0,
              paddingHorizontal: 15.0,
              selected: currentRoute == '/tutorials',
              closeDrawer: true,
              isLink: true,
            ),
            TextLink(
              S.of(context).support,
              '/my-support/${Constants.BUSINESS_ID}',
              paddingVertical: 10.0,
              paddingHorizontal: 15.0,
              selected: currentRoute == '/my-support/${Constants.BUSINESS_ID}',
              closeDrawer: true,
            ),
            TextLink(
              S.of(context).shop,
              '/shop',
              paddingVertical: 10.0,
              paddingHorizontal: 15.0,
              selected: currentRoute == '/shop',
              closeDrawer: true,
            ),
            TextLink(
              S.of(context).blog,
              '/blog/${Constants.BUSINESS_ID}',
              paddingVertical: 10.0,
              paddingHorizontal: 15.0,
              selected: currentRoute == '/blog/${Constants.BUSINESS_ID}',
              closeDrawer: true,
            ),
            _user != null ?
            (currentRoute == '/me') ?
            GestureDetector(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    child: Text(
                      S.of(context).logout,
                    ),
                  ),
                  Icon(
                    Icons.logout,
                  )
                ],
              ),
              onTap: () {

              },
            ) : IconButton(
              icon: Icon(
                Icons.account_circle,
                color: Colors.lightBlueAccent,
              ),
              onPressed: () {
                Routes.router.navigateTo(context, '/me');
              },
            ) :
            TextLink(
              S.of(context).login,
              '/login',
              paddingVertical: 10.0,
              paddingHorizontal: 15.0,
              selected: currentRoute == '/login',
              closeDrawer: true,
            ),
            Container(
              margin: EdgeInsets.only(top: 20.0),
              height: 0.5,
              color: Colors.green,
            ),
            Container(
              margin: EdgeInsets.only(top: 20.0, bottom: 10.0),
              child: Text(
                S.of(context).information,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.black54,
                  fontSize: 16.0,
                ),
              ),
            ),
            TextLink(
              S.of(context).service_policy,
              '/service-policy',
              paddingVertical: 5.0,
              paddingHorizontal: 10.0,
              closeDrawer: true,
            ),
            TextLink(
              S.of(context).return_policy,
              '/return-policy',
              paddingVertical: 5.0,
              paddingHorizontal: 10.0,
              closeDrawer: true,
            ),
            TextLink(
              S.of(context).privacy_policy,
              '/privacy-policy',
              paddingVertical: 5.0,
              paddingHorizontal: 10.0,
              closeDrawer: true,
            ),
            TextLink(
              S.of(context).license_agreement,
              '/end-user-license-agreement',
              paddingVertical: 5.0,
              paddingHorizontal: 10.0,
              closeDrawer: true,
            ),
            Container(
              margin: EdgeInsets.only(top: 20.0, bottom: 10.0),
              child: Text(
                S.of(context).support,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.black54,
                  fontSize: 16.0,
                ),
              ),
            ),
            TextLink(S.of(context).wiki, '/wiki', paddingVertical: 5.0, paddingHorizontal: 10.0, closeDrawer: true,),
            TextLink(S.of(context).support_ticket, '/my-support/${Constants.BUSINESS_ID}', paddingVertical: 5.0, paddingHorizontal: 10.0, closeDrawer: true,),
            TextLink(S.of(context).contact_us, '/contact-us', paddingVertical: 5.0, paddingHorizontal: 10.0, closeDrawer: true,),
            TextLink(S.of(context).about_us, '/about-us', paddingVertical: 5.0, paddingHorizontal: 10.0, closeDrawer: true,),
            TextLink(S.of(context).renew_license, '/renew-license', paddingVertical: 5.0, paddingHorizontal: 10.0, closeDrawer: true,),
            TextLink(S.of(context).create_a_online_store, '/contact-stores', paddingVertical: 5.0, paddingHorizontal: 10.0,),
            Container(
              margin: EdgeInsets.only(top: 20.0, bottom: 10.0),
              child: Text(
                S.of(context).developer_of,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  color: Colors.black54,
                  fontSize: 16.0,
                ),
              ),
            ),
            Wrap(
              children: [
                Container(
                  padding: EdgeInsets.all(10.0),
                  child: Icon(
                    IconData(
                        Constants.FONT_GOOGLE,
                        fontFamily: 'wisetronic',
                        fontPackage: null
                    ),
                    color: Colors.black26,
                    size: 48.0,
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(10.0),
                  child: Icon(
                    IconData(
                        Constants.FONT_ALEXA,
                        fontFamily: 'wisetronic',
                        fontPackage: null
                    ),
                    color: Colors.black26,
                    size: 48.0,
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(10.0),
                  child: Icon(
                    IconData(
                        Constants.FONT_APPLE,
                        fontFamily: 'wisetronic',
                        fontPackage: null
                    ),
                    color: Colors.black26,
                    size: 48.0,
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(10.0),
                  child: Icon(
                    IconData(
                        Constants.FONT_EBAY,
                        fontFamily: 'wisetronic',
                        fontPackage: null
                    ),
                    color: Colors.black26,
                    size: 48.0,
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(10.0),
                  child: Icon(
                    IconData(
                        Constants.FONT_QUICKBOOKS,
                        fontFamily: 'wisetronic',
                        fontPackage: null
                    ),
                    color: Colors.black26,
                    size: 48.0,
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(10.0),
                  child: Icon(
                    IconData(
                        Constants.FONT_SHOPIFY,
                        fontFamily: 'wisetronic',
                        fontPackage: null
                    ),
                    color: Colors.black26,
                    size: 48.0,
                  ),
                ),
              ],
            ),
            Container(
              padding: EdgeInsets.only(left: 6.0, right: 6.0, top: 12.0, bottom: 18.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Container(
                    child: Text(
                      '© 2007-${DateTime.now().year} wisetronic.com. All Rights Reserved.',
                      style: TextStyle(
                        fontSize: 10.0,
                        color: Colors.black38,
                      ),
                      textAlign: TextAlign.center,
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 6.0),
                    child: Text(
                      'All logos shown are registered trademark, copyrighted and belong to their respective owners.',
                      style: TextStyle(
                        fontSize: 8.0,
                        color: Colors.black38,
                      ),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    eventBus.on<OnCurrentUserUpdated>().listen((event) {
      if (mounted) {
        setState(() {
          _user = store.state.user;
        });
      }
    });

    eventBus.on<OnGetCurrentUserFailed>().listen((event) {
      if (mounted) {
        setState(() {
          _user = null;
        });
      }
    });

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (mounted) {
        setState(() {
          _user = store.state.user;
        });
      }
    });
  }
}