import 'dart:convert' as convert;

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_data/model/mine_data_model.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_header/model/mine_profile_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class MineDataWidget extends StatelessWidget {
  final Map<String, dynamic> data;
  final MineProfileModel profile;
  Map<String, dynamic> _json;
  MineDataModel _style;

  MineDataWidget(this.profile,
      this.data, {
        Key key,
      }) : super(key: key) {
    String d = data['data'];
    _json = convert.jsonDecode(d);
    _style = MineDataModel.fromJson(Map<String, dynamic>.from(_json));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 12.5, right: 12.5),
      decoration: BoxDecoration(
          color: HexColor.fromHex(_style.gridViewBgColor),
          borderRadius: BorderRadius.circular(7.5)),
      child: Column(
        children: <Widget>[
          _createTop(context),
          _createCenter(),
        ],
      ),
    );
  }

  Widget _createTop(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 62,
      child: Stack(
        children: <Widget>[
          Container(
            width: double.infinity,
            height: double.infinity,
            child: ClipRRect(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(7.5),
                  topRight: Radius.circular(7.5)),
              child: CachedNetworkImage(
                imageUrl: _style.accumulatedEarningsBgImg,
                fit: BoxFit.cover,
              ),
            ),
          ),
          Container(
            width: double.infinity,
            height: double.infinity,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 15, right: 15),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          '累计收益',
                          style: TextStyle(
                            fontSize: 11,
                            fontWeight: FontWeight.w600,
                            color: HexColor.fromHex(
                                _style.accumulatedEarningsNameColor),
                          ),
                        ),
                        Text(
                          '${profile.total}',
                          style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.w800,
                            color: HexColor.fromHex(
                                _style.accumulatedEarningsNameColor),
                            fontFamily: 'Din-Bold'
                                ''
                                ''
                                ''
                                '',
                            package: 'zhiying_base_widget',
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                GestureDetector(
                  child: Container(
                    margin: EdgeInsets.only(right: 30),
                    width: 64,
                    height: 24,
                    child: Stack(
                      children: <Widget>[
                        CachedNetworkImage(
                          imageUrl: _style.btnImg,
                          fit: BoxFit.cover,
                        ),
                        Center(
                          child: Text(
                            _style.btnText,
                            style: TextStyle(
                                fontSize: 12,
                                color: HexColor.fromHex(_style.btnTextColor)),
                          ),
                        )
                      ],
                    ),
                  ),
                  onTap: () {
                    RouterUtil.route(_json, context);
                  },
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _createCenter() {
    List<MineProfileDataModel> datas = profile.datas;
    int maxColumn = 2;
    int row = (datas.length / 2).ceil();
    if (row % 3 == 0) {
      maxColumn = 3;
    }
    List<Widget> rows = List();
    List<Widget> columns = List();
    int i = 0;
    for (; i < row; i++) {
      rows.add(
        _createrCenterItem(datas[i].name, datas[i].value),
      );
      if (i % maxColumn == maxColumn - 1 || i == row - 1) {
        columns.add(Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: rows,
        ));
        rows = List();
      } else {
        rows.add(Container(
          height: 33,
          width: 0.5,
          color: HexColor.fromHex('#ebebeb'),
        ));
      }
    }

    if (i < datas.length) {
      columns.add(Container(
        height: 0.5,
        color: HexColor.fromHex('#ebebeb'),
      ));
    }

    for (; i < datas.length; i++) {
      rows.add(
        _createBottomItem(datas[i].name, datas[i].value),
      );
      if ((i - row) % maxColumn == maxColumn - 1 ||
          i == row - 1 ||
          i == datas.length - 1) {
        columns.add(Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: rows,
        ));
        rows = List();
      } else {
        rows.add(Container(
          height: 33,
          width: 0.5,
          color: HexColor.fromHex('#ebebeb'),
        ));
      }
    }

    return Container(
      width: double.infinity,
      child: Column(
        children: columns,
      ),
    );
  }

  Widget _createrCenterItem(String name, String value) {
    return Expanded(
      child: Container(
        padding: EdgeInsets.only(left: 15, right: 15),
        height: 72,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              name,
              style: TextStyle(
                  fontSize: 11,
                  color: HexColor.fromHex(_style.gridViewNameColor)),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 2),
              child: Text(
                value,
                style: TextStyle(
                    fontSize: 18,
                    color: HexColor.fromHex(_style.gridViewValueColor),
                    fontFamily: 'Din-Bold',
                    package: 'zhiying_base_widget'),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _createBottomItem(String name, String value) {
    return Expanded(
      child: Container(
        padding: EdgeInsets.only(left: 15, right: 15),
        height: 46,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text(
              name,
              style: TextStyle(
                  fontSize: 12,
                  color: HexColor.fromHex(_style.gridViewNameColor)),
            ),
            Padding(
              padding: const EdgeInsets.only(left: 2),
              child: Text(
                value,
                style: TextStyle(
                    fontSize: 12,
                    color: HexColor.fromHex(_style.gridViewValueColor),
                    fontFamily: 'Din-Bold',
                    package: 'zhiying_base_widget'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
