import 'dart:convert' as convert;

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:zhiying_base_widget/widgets/home/home_auth/models/home_auth_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class HomeAuth extends StatelessWidget {
  final Map<String, dynamic> data;
  Map<String, dynamic> _json;
  HomeAuthModel _style;
  UserInfoModel _user;

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

  @override
  Widget build(BuildContext context) {
    _user = Provider.of<UserInfoNotifier>(context).userInfo;
    return Container(
      height: 34,
      width: double.infinity,
      margin: EdgeInsets.only(left: 12, right: 12, top: 4, bottom: 4),
      decoration: BoxDecoration(
          color: HexColor.fromHex(_style?.taoaboTipBgColor ?? 'ffffff'),
          borderRadius: BorderRadius.circular(17),
          boxShadow: [
            BoxShadow(
                offset: Offset(2, 1), //x,y轴
                color:
                    HexColor.fromHex(_style?.taoaboTipShadowColor ?? '767676')
                        .withAlpha(70), //投影颜色
                blurRadius: 5 //投影距离
                )
          ]),
      child: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(left: 10, right: 8),
            width: 20,
            height: 20,
            child: CachedNetworkImage(
              imageUrl: _style.taobaoAuthIcon,
              fit: BoxFit.contain,
            ),
          ),
          Expanded(
            child: Text(
              _style?.taoaboTipText ?? '',
              maxLines: 1,
              style: TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.bold,
                color: HexColor.fromHex(_style?.taoaboTipTextColor ?? '333333'),
              ),
            ),
          ),
          GestureDetector(
            child: Container(
              padding: EdgeInsets.only(left: 12, right: 12, top: 3, bottom: 3),
              margin: EdgeInsets.only(left: 8, right: 8),
              decoration: BoxDecoration(
                  color:
                      HexColor.fromHex(_style?.taoaboTipBtnBgColor ?? 'FF4242'),
                  borderRadius: BorderRadius.circular(20)),
              child: Text(
                _style?.taoaboTipBtnText ?? '',
                style: TextStyle(
                  fontSize: 12,
                  color: HexColor.fromHex(
                      _style?.taoaboTipBtnTextColor ?? 'ffffff'),
                ),
              ),
            ),
            onTap: () async {
              if (_user?.token == null || _user.token == '') {
                print('need login...');
                RouterUtil.goLogin(context);
                return;
              }

              bool isAuth = await TaobaoAuth.isAuth();
              if (!isAuth) {
                TaobaoAuth.auth(context);
              } else {
                Logger.debug('您已经授权过了');
              }
            },
          ),
        ],
      ),
    );
  }
}
