import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sport/application.dart';
import 'package:sport/provider/login_info_model.dart';
import 'package:sport/provider/user_model.dart';
import 'package:sport/widgets/dialog/alert_dialog.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/misc.dart';

const List<String> RANK_AREA = ["全国榜", "全省榜", "全市榜"];

class AreaPage extends StatefulWidget {
  final String area;

  AreaPage({this.area});

  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<AreaPage> {
  String _rank;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _rank = widget.area ?? RANK_AREA[0];
    return PopupMenuButton(
      onSelected: (v) async {
        var userModel = Provider.of<UserModel>(context, listen: false);
        var user = userModel.user;
        if (v != RANK_AREA[0]) {
          if (user.city?.isEmpty == true) {
            await userModel.initUser();
            user = userModel.user;
          }
          if (user.city?.isEmpty == true) {
            if (await showDialog(
                  context: context,
                  builder: (context) => CustomAlertDialog(title: '是否获取你的当前位置', ok: () => Navigator.of(context).pop(true)),
                ) ==
                true) {
              await request(context, () async {
                Location location = await Application.location();
                print(location);
                if (location != null) {
                  await new LoginInfoModel().updateUserInfo(context, user.name, user.gender, districtId: int.parse(location.adCode));
                  user = userModel.user;
                }
              });
            } else {
              return;
            }
          }
        }

        if (_rank == v) return;

        _rank = v;

        bool all = _rank == RANK_AREA[0];
        bool province = _rank == RANK_AREA[1];
        AreaNotification(_rank, all, province, user.provinceId, user.cityId, user.districtId).dispatch(context);

        setState(() {});
      },
      itemBuilder: (BuildContext context) {
        return divideMenus(RANK_AREA
            .map((e) => menuItemSelected(e, e, e == _rank))
            .toList());
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            _rank, style: Theme.of(context).textTheme.bodyText1
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(6, 6, 12.0, 6),
            child: arrowBottom(),
          )
        ],
      ),
    );
  }
}

class AreaNotification extends Notification {
  String rank;
  bool all, province;
  int provinceId, cityId, districtId;

  AreaNotification(this.rank, this.all, this.province, this.provinceId, this.cityId, this.districtId);
}
