import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/sport_target.dart';
import 'package:sport/bean/sport_target_today.dart';
import 'package:sport/pages/home/target_modify.dart';
import 'package:sport/pages/social/notification.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/services/api/resp.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/box.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/label.dart';
import 'package:sport/widgets/misc.dart';
import 'package:sport/widgets/space.dart';

class TargetModifyPage extends StatefulWidget {
  @override
  createState() => _PageState();
}

class _PageState extends State<TargetModifyPage> with InjectApi {
  Space space = Space(height: 5);
  ValueNotifier<SportTargetToday> _targetValueNotifier = ValueNotifier(null);
  bool _offstage = true;
  SportTarget _sportTarget;
  ScrollController _scrollController;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    api.getSportTargetMy().then((value) {
      _targetValueNotifier.value = value.data;
      setState(() {});
    });
  }

  @override
  void dispose() {
    _scrollController?.dispose();
    _targetValueNotifier?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: NotificationListener(
      onNotification: (notification) {
        if (notification is ScrollToBottomNotification) {
          Future.delayed(Duration(milliseconds: 500)).then((value) => _scrollController.jumpTo(_scrollController.position.maxScrollExtent));
        }
        return true;
      },
      child: CustomScrollView(
        controller: _scrollController,
        slivers: <Widget>[
          buildSliverAppBar(context, "修改运动目标", useClose: true, backgroundColor: Theme.of(context).scaffoldBackgroundColor, actions: <Widget>[
            IconButton(
              icon: Image.asset("lib/assets/img/topbar_ok.png"),
              onPressed: () async {
                if (_sportTarget == null) return;
                SportTarget sportTarget = _sportTarget;
                RespData<SportTargetToday> resp = (sportTarget.name == '自定义'
                    ? await api.setSportTargetCustom(sportTarget.duration, sportTarget.consume)
                    : await api.setSportTarget(sportTarget.id));
                if (resp.code == 0) {
                  Navigator.of(context).pop(resp.data);
                }
              },
            )
          ]),
          SliverToBoxAdapter(
            child: ValueListenableBuilder(
              valueListenable: _targetValueNotifier,
              builder: (_, value, __) => value == null ? Container() : _buildCurrentWidget(value),
            ),
          ),
          SliverToBoxAdapter(
            child: _buildTargetWidget(context),
          ),
        ],
      ),
    ));
  }

  Widget _buildCurrentWidget(SportTargetToday item) {
    SportTarget target = item.target;
    return target == null
        ? Container()
        : Column(
            children: <Widget>[
              LabelWidget(title: "当前运动目标"),
              BoxWidget(
                body: Column(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          "目标强度：",
                          style: Theme.of(context).textTheme.subtitle1,
                        ),
                        Text(target.intension, style: Theme.of(context).textTheme.subtitle1)
                      ],
                    ),
                    space,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          "每日目标时长：",
                          style: Theme.of(context).textTheme.subtitle1,
                        ),
                        Text("${target.duration}分钟", style: Theme.of(context).textTheme.subtitle1)
                      ],
                    ),
                    space,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          "每日预计消耗：",
                          style: Theme.of(context).textTheme.subtitle1,
                        ),
                        Text("${target.consume}kal", style: Theme.of(context).textTheme.subtitle1)
                      ],
                    ),
                    space,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          "适用人群：",
                          style: Theme.of(context).textTheme.subtitle1,
                        ),
                        Text(target.fitPeople, style: Theme.of(context).textTheme.subtitle1)
                      ],
                    ),
                    space,
                    space,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Expanded(
                          child: Divider(
                            endIndent: 24.0,
                          ),
                        ),
                        Text(
                          "相当于",
                          style: Theme.of(context).textTheme.bodyText1,
                        ),
                        Expanded(
                          child: Divider(
                            indent: 24.0,
                          ),
                        )
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 10.0),
                      child: sportBeEquivalentTo(context, target.consume),
                    ),
                  ],
                ),
              ),
            ],
          );
  }

  Widget _buildTargetWidget(BuildContext context) {
    return NotificationListener<TargetNotification>(
      onNotification: (notification) {
        _sportTarget = notification.target;
        return true;
      },
      child: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 8.0),
            child: LabelWidget(title: "设立新运动目标"),
          ),
          TargetModify(target: _targetValueNotifier.value == null ? null : _targetValueNotifier.value.target)
        ],
      ),
    );
  }
}
