import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/logger.dart';
import 'package:aman_kassa_flutter/core/models/choice.dart';
import 'package:aman_kassa_flutter/core/models/response.dart';
import 'package:aman_kassa_flutter/core/route_names.dart';
import 'package:aman_kassa_flutter/core/services/ApiService.dart';
import 'package:aman_kassa_flutter/core/services/DataService.dart';
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
import 'package:aman_kassa_flutter/redux/actions/user_actions.dart';
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
import 'package:aman_kassa_flutter/redux/state/setting_state.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:aman_kassa_flutter/views/home/components/header_title.dart';
import 'package:aman_kassa_flutter/widgets/loader/Dialogs.dart';

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:logger/logger.dart';

import './tabs/KassaTab.dart';
import './tabs/AdditionalTab.dart';
import './tabs/CalculatorTab.dart';

import './components/popup_menu.dart';
import 'components/bottom_bar.dart';

class HomeView extends StatefulWidget {
  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  Logger log = getLogger('HomeView');
  PageController pageController;
  int selectedTabIndex;
  DataService _dataService = locator<DataService>();
  ApiService _api = locator<ApiService>();
  NavigatorService _navigatorService = locator<NavigatorService>();
  final GlobalKey<State> _keyLoader = new GlobalKey<State>();

  @override
  void initState() {
    super.initState();
    selectedTabIndex = 0;
    pageController = new PageController(initialPage: selectedTabIndex);
    Redux.store.dispatch(checkSmena);
    _dataService.checkDbFill(Redux.store.state.userState.user);
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }

  void _onSelectChoice(Choice choice) async {
    if (choice.command == 'exit') {
      Dialogs.showLoadingDialog(context, _keyLoader);
      Response<dynamic> result =
      await _api.logout(Redux.store.state.userState.user.token);
      if (result.operation && result.status == 200) {
        Redux.store.dispatch(logoutAction);
      }
      Navigator.of(_keyLoader.currentContext, rootNavigator: true).pop();
    } else if (choice.command == 'infokkm') {
      _navigatorService.push(InfoKkmViewRoute);
    } else if (choice.command == 'bank') {
      _navigatorService.push(BankViewRoute);
    } else if (choice.command == 'tap2phone') {
      _navigatorService.push(BankSettingViewRoute);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        titleSpacing: 5.0,
        brightness: Brightness.light,
        title: HeaderTitle(),
        actions: <Widget>[
          PopupMenu(
            onSelectChoice: _onSelectChoice,
          )
        ],
        backgroundColor: fillColor,
      ),
      body: StoreConnector<AppState, SettingState>(
          converter: (store) => store.state.settingState,
          builder: (context, vm) {
            return PageView(
              onPageChanged: (index) {
                setState(() {
                  selectedTabIndex = index;
                });
              },
              controller: pageController,
              children: <Widget>[
                vm.mode == SettingModeKassa ? KassaTab(0) : CalculatorTab(0),
                AdditionalTab(1),
              ],
            );
          }),
      bottomNavigationBar: BottomBar(
        pageController: pageController,
        selectedTabIndex: selectedTabIndex,
      ),
    );
  }
}
