import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/style.dart';
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/colors.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/res/iconfont.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/utils/url.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flustars/flustars.dart' as FlutterStars;
import 'package:fluwx/fluwx.dart' as fluwx;

class NewsDetailPage extends StatefulWidget {
  NewsDetailPage(this.id);

  final String id;

  @override
  State<StatefulWidget> createState() {
    return NewsDetailPageState();
  }
}

class NewsDetailPageState extends State<NewsDetailPage> {
  dynamic item;

  @override
  void initState() {
    super.initState();
    getNewsDetail();
  }

  getNewsDetail() {
    print(666);
    NewApiService().newsDetail(widget.id, onSuccess: (res) {
      if (res != null) {
        _hasData = true;
        item = res;
      }

      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  zanClick() {
    ApiService(context: context).newsZan(widget.id, onSuccess: (res) {
      item.isLike = item.isLike ? false : true;
      item.likeNum = "${int.parse(item.likeNum) + (item.isLike ? 1 : -1)}";
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  bool _hasData = false;

  showAlertEvent() {
    showAlert(
      context,
      "提示",
      "确定登录？",
      "确定",
      () {
        NavigatorUtils.push(context, AccountRouter.loginPage, clearStack: true);
      },
      txt2: "取消",
      onPre2: () {
        NavigatorUtils.goBack(context);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    bool isDark = ThemeUtils.isDark(context);
    return Scaffold(
      backgroundColor: ThemeUtils.getTabsBg(context),
      appBar: MyAppBar(
        centerTitle: "新闻详情",
        actions: [
          GestureDetector(
            onTap: () {
              if (FlutterStars.SpUtil.getString(Constant.userId) == "-1") {
                showAlertEvent();
              } else {
                String initThis = randomInt(1111, 9999).toString() +
                    DateTime.now().millisecondsSinceEpoch.toString();

                fluwx
                    .shareToWeChat(fluwx.WeChatShareWebPageModel(
                      scene: fluwx.WeChatScene.SESSION,
                      webPage:
                          "$jumpUrl/h5/index.html?num=$initThis&page=/news/detail&id=${widget.id}",
                      title: item.title,
                      // description: item.descr,
                      thumbnail: '',
                    ))
                    .then((result) {}, onError: (msg) {});
              }
            },
            child: Icon(
              Iconfont.fenxiang,
            ),
          )
        ],
      ),
      body: SafeArea(
        child: _hasData
            ? ListView(
                padding: EdgeInsets.fromLTRB(15, 18, 15, 18),
//              mainAxisAlignment: MainAxisAlignment.start,
//              crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "${item.title}",
                    style: TextStyle(
                        fontSize: 20,
                        color: isDark ? Color(0xff333333) : Colours.text,
                        fontWeight: FontWeight.w500),
                  ),
                  Container(
                    padding: EdgeInsets.only(top: 10, bottom: 10),
                    child: Row(
                      children: <Widget>[
                        Text(
                          "来源：${item.releaseUser}·${item.releaseDate}",
                          style:
                              TextStyle(fontSize: 12, color: Color(0xFF999999)),
                        ),
                      ],
                    ),
                  ),
                  Html(
                    data: item.content,
                    style: {
                      "html": Style(width: width),
                      "p": Style(fontSize: FontSize(ScreenUtil().setSp(15)))
                    },
                  ),
                  Row(
                    children: <Widget>[
                      Text(
                        "${item.lookNum}阅读·${item.likeNum}赞",
                        style:
                            TextStyle(fontSize: 11, color: Color(0xffCCCCCC)),
                      ),
                    ],
                  ),
                  Gaps.vGap15,
                  Center(
                    child: GestureDetector(
                      onTap: () {
                        zanClick();
                      },
                      child: Column(
                        children: <Widget>[
                          LoadAssetImage(
                            "news/${item.isLike ? 'zan_yes' : 'zan_no'}",
                            width: 21,
                            height: 21,
                          ),
                          Text(
                            "${item.isLike ? '已赞' : '点赞'}",
                            style: TextStyle(
                              fontSize: 13,
                              color: Color(0xff666666),
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              )
            : Center(
                child: Text("正在加载..."),
              ),
      ),
    );
  }
}
