import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:satu/core/redux/actions/nav_actions.dart';
import 'package:satu/core/redux/actions/user_actions.dart';
import 'package:satu/core/redux/store.dart';

import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'package:satu/views/settings/setting_view.dart';
import 'package:satu/views/work/work_view.dart';

class AppDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          _createHeader(),
          _createDrawerItem(
              icon: Icons.contacts,
              text: 'Касса',
              onTap: () {
                Navigator.of(context).pop();
                Redux.store!.dispatch(navigateDrawer(WorkView));
              }),
          const Divider(),
          ExpansionTile(
            title: Text('Справочники'), // ignore: prefer_const_constructors
            childrenPadding: const EdgeInsets.only(left: 18.0),
            children: <Widget>[
              ListTile(
                title: const Text('Категории'),
                onTap: () {
                  Navigator.of(context).pop();
                },
              ),
              ListTile(
                title: const Text('Товары'),
                onTap: () {
                  Navigator.of(context).pop();
                },
              ),
              ListTile(
                title: const Text('Контрагенты'),
                onTap: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
          _createDrawerItem(
              icon: Icons.settings,
              text: 'Настройки',
              onTap: () {
                Navigator.of(context).pop();
                Redux.store!.dispatch(navigateDrawer(SettingsView));
              }),
          Divider(),
          _createDrawerItem(icon: Icons.bug_report, text: 'Сообщить об ошибке'),
          verticalSpaceMedium,
          _createDrawerItem(
              icon: Icons.exit_to_app,
              text: 'Выйти из аккаунта',
              onTap: () async {
                Redux.store!.dispatch(logout);
              }),
          ListTile(
            title: Text('0.0.1'),
          ),
        ],
      ),
    );
  }

  Widget _createHeader() {
    return SizedBox(
      height: 180.h,
      child: DrawerHeader(
          margin: EdgeInsets.zero,
          padding: EdgeInsets.zero,
          decoration: const BoxDecoration(
              image: DecorationImage(
                  fit: BoxFit.cover,
                  image: AssetImage('assets/images/drawer/background.png'))),
          child: Stack(children: <Widget>[
            Positioned(
                bottom: 12.0,
                left: 16.0,
                child: Row(
                  children: [
                    SizedBox(
                      height: 40,
                      width: 40,
                      child: Container(
                          decoration: const BoxDecoration(
                              image: DecorationImage(
                                  fit: BoxFit.cover,
                                  image: AssetImage(
                                      'assets/images/drawer/user.png')))),
                    ),
                    horizontalSpaceSmall,
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: const [
                        Text('Хайруллин Тимур',
                            style: TextStyle(fontSize: 16.0)),
                        Text('Продавец', style: TextStyle(fontSize: 12)),
                      ],
                    ),
                  ],
                )),
          ])),
    );
  }

  Widget _createDrawerItem(
      {required IconData icon,
      required String text,
      GestureTapCallback? onTap}) {
    return ListTile(
      title: Row(
        children: <Widget>[
          Icon(icon),
          Padding(
            padding: EdgeInsets.only(left: 8.0),
            child: Text(text),
          )
        ],
      ),
      onTap: onTap,
    );
  }
}
