import 'dart:convert' as convert;
import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MineNavDelegate extends SliverPersistentHeaderDelegate {
  final Map<String, dynamic> data;
  double _height;
  Color _color;

  List<Widget> _icons = List();
  double marginTop;
  double marginLeftRight;

  MineNavDelegate(this.data) : super() {
    _height = MediaQueryData.fromWindow(window).padding.top + 44;
    String d = data['data'];
    Map<String, dynamic> json = convert.jsonDecode(d);
    if (json.containsKey('app_bar_bg_color')) {
      _color = HexColor.fromHex(json['app_bar_bg_color']);
    } else {
      _color = Colors.amber;
    }
    if (json.containsKey('list_style')) {
      List icons = json['list_style'];
      _icons = icons.map((icon) {
        SkipModel model = SkipModel.fromJson(Map<String, dynamic>.from(icon));
        return MineNavIcon(icon['icon'], model);
      }).toList();
    }
    marginTop = 0;
    if (json.containsKey("is_top_margin") && json.containsKey("top_margin")) {
      marginTop = json['is_top_margin'] == "1" ? double.parse(json['top_margin']) : 0;
    }
    marginLeftRight = 0;
    if (json.containsKey("is_left_right_margin") && json.containsKey("left_right_margin")) {
      marginLeftRight = json['is_left_right_margin'] == "1" ? double.parse(json['left_right_margin']) : 0;
    }
  }

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    double percent = shrinkOffset / _height;
    return MineNav(_color.withOpacity(percent), _icons, marginTop, marginLeftRight);
  }

  @override
  double get maxExtent => _height;

  @override
  double get minExtent => _height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) => false; // 如果内容需要更新,设置为true
}

class MineNav extends StatelessWidget {
  final Color color;
  final List<Widget> widgets;
  double marginLeftRight;
  double marginTop;

  MineNav(this.color, this.widgets, this.marginTop, this.marginLeftRight);

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.only(top: marginTop, left: marginLeftRight, right: marginLeftRight),
        padding: EdgeInsets.only(left: 12, right: 12),
        color: color,
        child: Column(
          children: <Widget>[
            Expanded(child: Container()),
            Container(
              width: double.infinity,
              height: 44,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: this.widgets,
              ),
            )
          ],
        ));
  }
}

class MineNavIcon extends StatelessWidget {
  final String imgUrl;
  final SkipModel skipModel;

  const MineNavIcon(this.imgUrl, this.skipModel, {Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        child: Container(
          width: 24,
          height: 24,
          margin: EdgeInsets.only(left: 6),
          child: CachedNetworkImage(
            imageUrl: imgUrl,
          ),
        ),
        onTap: () {
          RouterUtil.route(skipModel, skipModel.toJson(), context);
        });
  }
}