import 'dart:convert';

import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/services/BankService.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';

class BankSettingView extends StatefulWidget {
  BankSettingView();

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

class _BankSettingViewState extends State<BankSettingView> {
  TextEditingController _emailController;
  TextEditingController _passwordController;
  final BankService _bankService = locator<BankService>();
  final DialogService _dialogService = locator<DialogService>();

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

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

    }
  }


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

  void _saveData() async {
    await Redux.store.dispatch(saveData(_emailController.text, _passwordController.text));
    _dialogService.showDialog(description: 'Данные сохранены');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Настройка Tap2Phone'),
      ),
      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', hintText: "Введите адрес почты"),
                keyboardType: TextInputType.emailAddress,
              ),
              TextField(
                controller: _passwordController,
                obscureText: true,
                decoration: InputDecoration(
                    labelText: 'Пароль', hintText: "Введите пароль"),
              ),
              verticalSpaceMedium,
              RaisedButton(
                onPressed: this._saveData,
                child: Text(
                  'Cохранить',
                  style: TextStyle(color: whiteColor, fontSize: 25.0),
                ),
                color: primaryColor,
                padding:
                    const EdgeInsets.symmetric(vertical: 5.0, horizontal: 20.0),
              )
            ],
          ),
        ),
      ),
    );
  }
}
