import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/models/choice.dart';
import 'package:aman_kassa_flutter/core/services/BankService.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';

class PopupMenu extends StatefulWidget {

  final void Function(Choice value) onSelectChoice;

  PopupMenu({required this.onSelectChoice});

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

class _PopupMenuState extends State<PopupMenu> {
  BankService _bankService = locator<BankService>();
  List<Choice> choices = <Choice>[];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    load();
  }

  load () async {
    int version = await _bankService.version();

    List<Choice> _choices = <Choice>[
      const Choice(title: 'Информация о ККМ', icon: Icons.info_outline, command: 'infokkm'),
      //if (version >= 24 )
      // const Choice(title: 'Bank', icon: Icons.text_fields, command: 'bank'),
      if (version >= _bankService.sdkVersion )
      const Choice(title: 'Настройка HalykPos', icon: Icons.phonelink_lock_outlined, command: 'tap2phone'),
      const Choice(title: 'Настройка FortePos', icon: Icons.phonelink_lock_outlined, command: 'fortepos'),
      const Choice(title: 'Настройки', icon: Icons.settings, command: 'settings'),
      const Choice(title: 'Принтер', icon: Icons.print, command: 'print'),
      const Choice(title: 'Выйти', icon: Icons.exit_to_app, command: 'exit')
    ];
    setState(() {
      choices= _choices;
    });
  }

  @override
  Widget build(BuildContext context) {
    return PopupMenuButton<Choice>(
      icon: Icon(
        Icons.more_vert,
        color: menuColor,
      ),
      onSelected: widget.onSelectChoice,
      itemBuilder: (BuildContext context) {
        return choices.map((Choice choice) {
          return PopupMenuItem<Choice>(
            value: choice,
            child: Row(children: <Widget>[
              Icon(choice.icon, color: primaryColor,),
              SizedBox(width: 5,),
              Text(choice.title)
            ], ),
          );
        }).toList();
      },
    );
  }
}
