import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
import 'package:aman_kassa_flutter/redux/actions/setting_actions.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/shared/ui_helpers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class SettingView extends StatefulWidget {
  SettingView();

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

class _SettingViewState extends State<SettingView> {
  late TextEditingController _pinController;
  final DialogService _dialogService = locator<DialogService>();

  @override
  void initState() {
    super.initState();
    SettingState state = Redux.store!.state.settingState!;
    _pinController = new TextEditingController(text: state.pinCode);
  }




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

  void _setPinCode(BuildContext _context) async {
    FocusScope.of(_context).unfocus();
    var value = _pinController.text;
    if(value.isNotEmpty && value.length !=4){
      _dialogService.showDialog(description: 'Необходимо указать 4-х значный числовой код');
    } else {
      await Redux.store!.dispatch(changePinCodeFromSetting(_pinController.text));
      _dialogService.showDialog(description: 'Данные успешно сохранены');
    }
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Настройка приложения'),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 14.0),
          child: Column(
            children: <Widget>[
              verticalSpaceTiny,
              Text(
                'Для блокировки приложения пин-кодом, укажите 4-х значный числовой код',
                style: TextStyle(fontSize: 15.0),
                textAlign: TextAlign.center,
              ),
              verticalSpaceTiny,
              TextField(
                controller: _pinController,
                maxLength: 4,
                obscureText: true,
                decoration: InputDecoration(
                    labelText: 'Пин-код', hintText: "Укажите пин-код"),
                keyboardType: TextInputType.number,
              ),
              verticalSpaceMedium,
              ElevatedButton(
                onPressed: () => this._setPinCode(context),
                child: Text(
                  'Cохранить настройки',
                  style: TextStyle(color: whiteColor, fontSize: 20.0),
                ),
                style: ElevatedButton.styleFrom(
                  backgroundColor: primaryColor,
                  padding: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 20.0),
                ),
              ),

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