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 StatefulWidget {
  final Map<String, dynamic> data;
  Map<String, dynamic> _json;
  HomeAuthModel _model;

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

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

class _HomeAuthState extends State<HomeAuth> {
  UserInfoModel _user;
  bool _isAuth = false;

  @override
  void initState() {
    // TaobaoAuth.isAuth().then((isAuth) {
    //   setState(() =>  this._isAuth = isAuth);
    // });

    super.initState();
  }

  @override
  void didChangeDependencies() {
    _user = Provider.of<UserInfoNotifier>(context).userInfo;
    _isAuth = _user?.isTBAuth ?? false;
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    if (_isAuth) {
      // 授权过，不显示
      return Container();
    }
    return Container(
      height: 34,
      width: double.infinity,
      margin: EdgeInsets.only(left: 12, right: 12, top: 4, bottom: 4),
      decoration: BoxDecoration(
          color: HexColor.fromHex(!_isAuth ? widget._model?.listStyle?.before?.bgColor ?? 'ffffff' : widget?._model?.listStyle?.after?.bgColor ?? 'ffffff'),
          borderRadius: BorderRadius.circular(17),
          boxShadow: [
            BoxShadow(
                offset: Offset(2, 1), //x,y轴
                color: HexColor.fromHex('767676')
                    .withAlpha(70), //投影颜色
                blurRadius: 5 //投影距离
                )
          ]),
      child: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(left: 10, right: 8),
            width: 20,
            height: 20,
            child: CachedNetworkImage(
              imageUrl: !_isAuth ?  widget._model?.listStyle?.before?.auditImg ?? '' : widget?._model?.listStyle?.after?.auditImg ?? '',
              fit: BoxFit.contain,
            ),
          ),
          Expanded(
            child: Text(
              !_isAuth ? widget._model?.listStyle?.before?.contentText ?? '' : widget?._model?.listStyle?.after?.contentText ?? '',
              maxLines: 1,
              style: TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.bold,
                color: HexColor.fromHex(
                    !_isAuth ? widget._model?.listStyle?.before?.contentTextColor ?? '333333' : widget?._model?.listStyle?.after?.contentTextColor ?? '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(
                      !_isAuth ? widget._model?.listStyle?.before?.btnBgColor ?? 'FF4242' : widget?._model?.listStyle?.after?.btnBgColor ?? 'FF4242' ),
                  borderRadius: BorderRadius.circular(20)),
              child: Text(
                 !_isAuth ?  widget._model?.listStyle?.before?.btnText ?? '' : widget?._model?.listStyle?.after?.btnText ?? '',
                style: TextStyle(
                  fontSize: 12,
                  color: HexColor.fromHex(
                      !_isAuth ? widget._model?.listStyle?.before?.btnTextColor ?? 'ffffff' : widget?._model?.listStyle?.after?.btnTextColor ?? ''),
                ),
              ),
            ),
            onTap: () async {
              if (_user?.token == null || _user.token == '') {
                print('need login...');
                RouterUtil.goLogin(context);
                return;
              }
              TaobaoAuth.auth(context);
            },
          ),
        ],
      ),
    );
  }
}
