import 'dart:convert';

import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/models/aman_dao.dart';
import 'package:aman_kassa_flutter/core/services/ForteService.dart';
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
import 'package:aman_kassa_flutter/redux/actions/bank_actions.dart';
import 'package:aman_kassa_flutter/redux/state/bank_state.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:aman_kassa_flutter/shared/ui_helpers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../core/services/navigator_service.dart';

class ForteSettingView extends StatefulWidget {
  ForteSettingView();

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

class _ForteSettingViewState extends State<ForteSettingView> {
  late TextEditingController _emailController;
  late TextEditingController _passwordController;
  final NavigatorService _navigatorService = locator<NavigatorService>();
  final DialogService _dialogService = locator<DialogService>();
  late String _sessionType;



  @override
  void initState() {
    super.initState();
    BankState state = Redux.store!.state.bankState!;
    _emailController = new TextEditingController(text: state.login);
    _passwordController = new TextEditingController(text: state.password);
    //permissions();
    _sessionType = 'Forte';
  }

  // Future<void> permissions() async {
  //   try {
  //     await _bankService.permissions();
  //   } on PlatformException {
  //
  //   }
  // }


  @override
  void dispose() {
    _emailController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  void _saveData(BuildContext _context) async {
    FocusScope.of(_context).unfocus();
    await Redux.store!.dispatch(saveData(
        _emailController.text,
        _passwordController.text,
        sessionType: _sessionType
    ));
    _dialogService.showDialog(description: 'Данные сохранены');
    _navigatorService.pop();
  }



  @override
  Widget build(BuildContext context) {
    final BankState? state = Redux.store?.state.bankState;

    // Проверяем, активна ли Halyk-сессия
    if ((state?.login != null && state!.login!.isNotEmpty) ||
        (state?.password != null && state!.password!.isNotEmpty))  {
      if (state.sessionType != 'Forte') {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text('Настройка FortePos'),
          ),
          body: Center(
            child: Text(
              'У вас подключен терминал Halyk',
              style: TextStyle(fontSize: 16.0, color: Colors.grey),
            ),
          ),
        );
      }
    }

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Настройка FortePos'),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 14.0),
          child: Column(
            children: <Widget>[
              verticalSpaceTiny,
              Text(
                'Необходимо указать почту/номер и пароль для подключения к системе проведения платежей',
                style: TextStyle(fontSize: 15.0),
                textAlign: TextAlign.center,
              ),
              verticalSpaceTiny,
              TextField(
                controller: _emailController,
                decoration: InputDecoration(
                    labelText: 'E-Mail / Номер телефона в формете 77xx ', hintText: "Введите адрес почты/телефон"),
                keyboardType: TextInputType.emailAddress,
              ),
              TextField(
                controller: _passwordController,
                obscureText: true,
                decoration: InputDecoration(
                    labelText: 'Пароль', hintText: "Введите пароль"),
              ),
              verticalSpaceMedium,
              ElevatedButton(
                onPressed: () => this._saveData(context),
                child: Text(
                  'Cохранить',
                  style: TextStyle(color: whiteColor, fontSize: 25.0),
                ),
                style: ElevatedButton.styleFrom(
                  backgroundColor: primaryColor,
                  padding: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 20.0),
                ),
              ),

            ],
          ),
        ),
      ),
    );
  }
}
