import 'dart:convert' as convert;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/goods_share_editor/goods_share_editor.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/goods_share_image/goods_share_image_model.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/models/goods_share_temp_add_model.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/widgets/good_share_msg.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/widgets/goods_share_alert_content.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/widgets/goods_share_edit.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/widgets/goods_share_item.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/widgets/goods_share_link_widget.dart';
import 'package:zhiying_base_widget/widgets/public/custom_button/custom_bottom.dart';
import 'package:zhiying_base_widget/widgets/public/custom_button/custom_button_model.dart';
import 'package:zhiying_base_widget/widgets/share/models/share_data_model.dart';
import 'package:zhiying_base_widget/widgets/share/share_alert.dart';

class GoodsShareLink extends StatefulWidget {
  final Map<String, dynamic> model;

  final GoodsShareTempDataModel datas;

  const GoodsShareLink(this.model, this.datas, {Key key}) : super(key: key);

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

class _GoodsShareLinkState extends State<GoodsShareLink> {
  GoodsShareImageModel _style;
  Map<int, String> _images = Map();
  List<Widget> widgets = List();

  // 分享数据
  final ShareDataModel _shareModel = ShareDataModel();

  @override
  void initState() {
    String data = widget.model['data'];
    var d = convert.jsonDecode(data);
    _style = GoodsShareImageModel.fromJson(Map<String, dynamic>.from(d));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    widgets.clear();
    widgets.add(GoodsShareItem(
      imgHeader: _style.cssList.headerImage,
      imgCheck: _style.cssList.unChooseImage,
      imgCheckSelected: _style.cssList.chooseImage,
      enabled: false,
      child: GoodsShareLinkWidget(
        title: '小米10至尊纪念版5g手机120X变焦骁龙865小米官方旗舰店小米手机',
        desc: '【原价】3999元【券后3989元【用嗨如意再省】19.74元復製(FRH51Lu9hYF',
        imgGoods: '',
        icon: '',
        name: '智莺生活',
      ),
    ));
    widgets.add(GoodsShareItem(
      imgHeader: _style.cssList.headerImage,
      imgCheck: _style.cssList.unChooseImage,
      imgCheckSelected: _style.cssList.chooseImage,
      enabled: false,
      child: GoodsShareEdit(
        _style.content,
        onCopyClick: () {
          Fluttertoast.showToast(msg: '复制成功');
        },
        onEditorClick: () {
          Fluttertoast.showToast(msg: '正在开发中');
          return;
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => GoodsShareEditor()));
        },
      ),
      onSelected: (isSelected) {
        _shareModel.content = isSelected ? '' : _style.content;
      },
    ));
    for (String message in _style.customContent) {
      widgets.add(
        GoodsShareItem(
          imgHeader: _style.cssList.headerImage,
          imgCheck: _style.cssList.unChooseImage,
          imgCheckSelected: _style.cssList.chooseImage,
          enabled: false,
          child: GoodsShareMessage(message),
        ),
      );
    }

    return Column(
      children: <Widget>[
        Expanded(
          child: Container(
            margin: EdgeInsets.only(bottom: 4),
            padding: EdgeInsets.only(top: 10),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(8),
                    bottomRight: Radius.circular(8))),
            child: SingleChildScrollView(
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: widgets),
            ),
          ),
        ),
        _createBottom(),
      ],
    );
  }

  Widget _createBottom() {
    Map<String, dynamic> shareImages = _style.cssList.shareLink;
    if (shareImages == null) {
      return Container();
    }
    CustomButtonModel btnCopy = CustomButtonModel.fromJson(
        Map<String, dynamic>.from(shareImages['copy_btn']));
    CustomButtonModel btnShare = CustomButtonModel.fromJson(
        Map<String, dynamic>.from(shareImages['share_btn']));
    return SafeArea(
      child: Container(
        margin: EdgeInsets.only(left: 12.5, right: 12.5, top: 4, bottom: 4),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            btnCopy == null
                ? Container()
                : Container(
                    width: 110,
                    height: 36,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(18),
                    ),
                    child: CustomButton(btnCopy)),
            btnShare == null
                ? Container()
                : GestureDetector(
                    child: Container(
                        margin: EdgeInsets.only(left: 5),
                        width: 110,
                        height: 36,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(18),
                        ),
                        child: CustomButton(btnShare)),
                    onTap: () {
                      _showShare();
                    },
                  ),
          ],
        ),
      ),
    );
  }

  void _showShare() async {
    List<String> images = List();
    for (int index = 0; index < _style.customImage.length; index++) {
      if (_images.containsKey(index)) {
        images.add(_images[index]);
      }
    }
    if (images.length >= 0) {
      _shareModel.image = images;
    }
    showCupertinoModalPopup(
      context: context,
      builder: (context) => ShareAlert(
        _shareModel,
        '',
        child: GoodsShareAlertContent(),
      ),
    );
  }
}
