import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:zhiying_base_widget/pages/wechat_teacher_page/model/wechat_teacher_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:url_launcher/url_launcher.dart';

class WechatTeachetItem extends StatelessWidget {
  OfficalWxchatList itemStyleModel;
  WechatTeacherStyleModel styleModel;

  WechatTeachetItem(this.itemStyleModel, this.styleModel);

  /// 拷贝
  void _copyText(String text) async {
    if (!EmptyUtil.isEmpty(text)) {
      await Clipboard.setData(ClipboardData(text: text));
      Fluttertoast.showToast(msg: '复制成功~');
      String url = "weixin://";
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    } else {
      Fluttertoast.showToast(msg: '内容不能为空～');
    }
  }

  /// 长按保存图片
  void _longClickSaveImage(String url) async {
    Logger.log('长按保存图片');
    if (!EmptyUtil.isEmpty(url)) {


      var msg = await Download().fileToGallery(url);
      Fluttertoast.showToast(msg: msg);
    }
  }

  @override
  Widget build(BuildContext context) {

    return Container(
      margin: EdgeInsets.only(top: 8, bottom: 8),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                // Text(
                //   itemStyleModel?.text ?? '微信公众号 智莺生活公众号',
                //   style: TextStyle(
                //     fontSize: 14,
                //     color: Color(0xff3c3c3c),
                //     fontWeight: FontWeight.bold,
                //   ),
                // ),

                RichText(
                  text: TextSpan(children: [
                    TextSpan(
                        text: itemStyleModel?.text ?? '',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 14,
                            color: HexColor.fromHex(styleModel?.officalWxchat?.titleColor ?? '#FF3C3C3C'))),
                    TextSpan(
                        text: '  ${itemStyleModel?.subText ?? ''}',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 11,
                            color: HexColor.fromHex(styleModel?.officalWxchat?.titleColor ?? '#FF3C3C3C'))),
                  ]),
                ),

                Padding(
                  padding: const EdgeInsets.only(top: 8.0, bottom: 8),
                  child: Text(
                    '微信号:${itemStyleModel?.wechatAccount ?? ''}',
                    style: TextStyle(
                      fontSize: 13,
                      // color: Color(0xffff5050),
                      color: HexColor.fromHex(styleModel?.commStyle?.wechatAccountColor ?? '#FF5050'),
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () => _copyText(itemStyleModel?.wechatAccount),
                  child: UnconstrainedBox(
                    child: Container(
                      decoration: BoxDecoration(
                        // color: Color(0xffffdada),
                        color: HexColor.fromHex(styleModel?.bindTeacherDialog?.btnBgColor ?? '#FFDADA'),
                        borderRadius: BorderRadius.circular(20),
                      ),
                      padding: EdgeInsets.only(left: 9, right: 9, top: 4, bottom: 4),
                      child: Text(
                        styleModel?.bindTeacherDialog?.btnText ?? '复制去微信添加',
                        style: TextStyle(
                            fontSize: 13,
                            color: HexColor.fromHex(styleModel?.bindTeacherDialog?.btnTextColor ?? '#FF0000')),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          GestureDetector(
            onLongPress: () => _longClickSaveImage(itemStyleModel?.qrcode),
            child: Column(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(bottom: 4),
                  width: 55,
                  height: 55,
                  // color: Colors.redAccent,
                  child: CachedNetworkImage(
                    imageUrl: itemStyleModel?.qrcode ?? '',
                  ),
                ),
                Text(
                  styleModel?.officalWxchat?.qrcodeText ?? '长按保存二维码',
                  style: TextStyle(
                      fontSize: 13, color: HexColor.fromHex(styleModel?.officalWxchat?.qrcodeTextColor ?? '#999999')),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
