import 'dart:convert' as convert;

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_header/mine_header_bloc.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_header/mine_header_sk.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_header/model/mine_header_model.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_header/model/mine_profile_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MineHeaderContainer extends StatefulWidget {
  final Map<String, dynamic> data;
  Map<String, dynamic> json;
  MineHeaderModel staticModel;

  MineHeaderContainer(this.data, {Key key}) : super(key: key) {
    String d = data['data'];
    json = convert.jsonDecode(d);
    staticModel = MineHeaderModel.fromJson(json);
  }

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

class _MineHeaderContainerState extends State<MineHeaderContainer> {
  MineHeaderBloc _bloc;

  @override
  void initState() {
    _bloc = BlocProvider.of<MineHeaderBloc>(context);
    _bloc.loadData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<MineProfileModel>(
        stream: _bloc.outData,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          MineProfileModel profile = snapshot.data;
          return profile == null
              ? MineHeaderSkeleton()
              : Row(
                  children: <Widget>[
                    GestureDetector(
                      child: Container(
                        margin: EdgeInsets.only(
                            left: 20, top: 20, bottom: 20, right: 12),
                        width: 56,
                        height: 56,
                        decoration: BoxDecoration(
                            color: Color(0xfff3f3f3),
                            borderRadius: BorderRadius.circular(28)),
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(28),
                          child: CachedNetworkImage(
                            imageUrl: profile.avatar,
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      onTap: () async {
                        await RouterUtil.route(SkipModel.fromJson(widget.json),
                            widget.json, context);
                        _bloc.loadData();
                      },
                    ),
                    Expanded(
                        child: Container(
                      margin: EdgeInsets.only(right: 12),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Text(
                                profile.username,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    fontSize: 17,
                                    color: HexColor.fromHex(
                                        widget.staticModel.userNameColor)),
                              ),
                              Container(
                                height: 18,
                                margin: EdgeInsets.only(left: 6),
                                padding: EdgeInsets.only(left: 6, right: 6),
                                decoration: BoxDecoration(
                                    color: HexColor.fromHex(
                                        widget.staticModel.lvBgColor),
                                    borderRadius: BorderRadius.circular(9)),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 12,
                                      height: 12,
                                      margin: EdgeInsets.only(right: 2),
                                      child: CachedNetworkImage(
                                        imageUrl: profile.userLvIcon,
                                        fit: BoxFit.contain,
                                      ),
                                    ),
                                    Text(
                                      profile.userLvName,
                                      style: TextStyle(
                                          fontSize: 10,
                                          color: HexColor.fromHex(
                                              widget.staticModel.lvNameColor)),
                                    )
                                  ],
                                ),
                              ),
                            ],
                          ),
                          GestureDetector(
                            child: Container(
                              margin: EdgeInsets.only(top: 8),
                              child: Row(
                                children: <Widget>[
                                  Text(
                                    '邀请码：${profile.inviteCode}',
                                    maxLines: 1,
                                    style: TextStyle(
                                      fontSize: 13,
                                      color: HexColor.fromHex(
                                          widget.staticModel.userNameColor),
                                      fontFamily: 'Din',
                                      package: 'zhiying_base_widget',
                                    ),
                                  ),
                                  Container(
                                    width: 12,
                                    height: 12,
                                    margin: EdgeInsets.only(left: 2),
                                    child: CachedNetworkImage(
                                      imageUrl: widget.staticModel.coypeIcon,
                                      fit: BoxFit.contain,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              Fluttertoast.showToast(msg: '复制成功');
                              Clipboard.setData(
                                  ClipboardData(text: profile.inviteCode));
                            },
                          ),
                        ],
                      ),
                    ))
                  ],
                );
        });
  }
}
