import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/model/intellect_dialog_data_model.dart';
import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/model/no_goods_dialog_style_model.dart';
import 'package:zhiying_base_widget/widgets/home/home_quick_entry/cached_network_image_util.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class IntellectSearchNoGoodsDialog extends StatefulWidget {
  final IntellectDialogDataModel model;

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

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

class _IntellectSearchNoGoodsDialogState
    extends State<IntellectSearchNoGoodsDialog> {
  TextEditingController _textEditingController;

  NoGoodsDialogStyleModel _styleModel;

  @override
  void initState() {
    _textEditingController = TextEditingController();
    _getClipBoardData();

    super.initState();
  }

  ///获取粘贴板内容
  _getClipBoardData() async {
    ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
    _textEditingController.text = data.text;
    requestStyle();
    setState(() {});
  }

  void requestStyle() {
    NetUtil.request("/api/v1/cfg/clipboard_unidentified", onSuccess: (data) {
      _styleModel = NoGoodsDialogStyleModel.fromJson(
          json.decode(data['clipboard_unidentified']));
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: InkWell(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Stack(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(top: 38.h, left: 92.w, right: 92.w),
                    padding: EdgeInsets.only(
                        top: (38 + 15).h,
                        left: 30.w,
                        right: 30.w,
                        bottom: 48.h),
                    decoration: BoxDecoration(
                        color: HexColor.fromHex(_styleModel?.bgColor ?? ""),
                        borderRadius: BorderRadius.circular(16)),
                    width: double.infinity,
                    child: Column(
                      children: <Widget>[_buildTopItem(), _buildBottomItem()],
                    ),
                  ),
                  Container(
                    height: 76.h,
                    child: Center(
                        child: Container(
                      height: 76.h,
                      width: 216.w,
                      child: CachedNetworkImage(
                        imageUrl: _styleModel?.titleImg ?? "",
                      ),
                    )),
                  ),
                ],
              )
            ],
          ),
        ));
  }

  _buildTopItem() {
    return Container(
      decoration: BoxDecoration(),
      child: Column(
        children: <Widget>[
          Text(
            _styleModel?.moduleList?.title?.content ?? "",
            style: TextStyle(
                color: HexColor.fromHex(
                    _styleModel?.moduleList?.title?.textColor ?? ""),
                fontSize: 30.sp),
          ),
          Container(
            margin: EdgeInsets.only(top: 12),
            decoration: BoxDecoration(
                color: HexColor.fromHex(
                    _styleModel?.moduleList?.content?.bgColor ?? ""),
                borderRadius: BorderRadius.circular(10)),
            child: TextField(
              controller: _textEditingController,
              textAlign: TextAlign.start,
              style: TextStyle(
                  color: HexColor.fromHex(
                      _styleModel?.moduleList?.content?.textColor ?? ''),
                  fontSize: 22.sp),
              decoration: InputDecoration(
                contentPadding: EdgeInsets.only(left: 10, top: 10, bottom: 4),
                border: InputBorder.none,
              ),
              maxLines: 8,
            ),
          )
        ],
      ),
    );
  }

  _buildBottomItem() {
    return Column(
      children: <Widget>[
        SizedBox(
          height: 24,
        ),
        Row(
          children: <Widget>[
            Expanded(
                child: Container(
              decoration: BoxDecoration(
                  color: HexColor.fromHex(
                      _styleModel?.moduleList?.btn?.bgColor ?? ""),
                  borderRadius: BorderRadius.circular(50)),
              child: Center(
                  child: Padding(
                padding: const EdgeInsets.all(12.0),
                child: Text(
                  _styleModel?.moduleList?.btn?.content ?? "",
                  style: TextStyle(
                      color: HexColor.fromHex(
                          _styleModel?.moduleList?.btn?.textColor),
                      fontSize: 24.sp),
                ),
              )),
            ))
          ],
        ),
        SizedBox(
          height: 16,
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Container(
              height: 1,
              width: 137.w,
              color: HexColor.fromHex(
                  _styleModel?.moduleList?.segment?.textColor ?? ""),
            ),
            Text(
              _styleModel?.moduleList?.segment?.content ?? "",
              style: TextStyle(
                  color: HexColor.fromHex(
                      _styleModel?.moduleList?.segment?.textColor ?? "")),
            ),
            Container(
              height: 1,
              width: 137.w,
              color: HexColor.fromHex(
                  _styleModel?.moduleList?.segment?.textColor ?? ""),
            ),
          ],
        ),
        SizedBox(
          height: 24,
        ),
        Container(
          height: 100.w,
          child: ListView.builder(
              scrollDirection: Axis.horizontal,
              shrinkWrap: true,
              itemCount: _styleModel?.iconList?.length ?? 0,
              itemBuilder: _buildBottomListItem),
        )
      ],
    );
  }

  Widget _buildBottomListItem(BuildContext context, int index) {
    var item = _styleModel.iconList[index];

    return Container(
        margin: EdgeInsets.only(left: 11, right: 11),
        child: InkWell(
          child: Column(
            children: <Widget>[
              Container(
                color: Colors.deepOrange,
                child: Image.network(
                  "",
                  width: 60.w,
                  height: 60.w,
                  fit: BoxFit.fill,
                ),
              ),
              SizedBox(
                height: 2,
              ),
              Text(
                item.title ?? "",
                style: TextStyle(
                    fontSize: 22.sp,
                    color: HexColor.fromHex(item.textColor ?? "")),
              )
            ],
          ),
          onTap: () {},
        ));
  }
}
