/*
 * @Author: WuWei
 * @Date: 2020-05-14 11:42:05
 * @LastEditors: wjc
 * @LastEditTime: 2023-03-30 15:34:36
 * @Description:
 */

import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/sass_news_detail_store.dart';

class SassDetailPage extends StatelessWidget {
  const SassDetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => SassNewsDetailStore(context),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SassNewsDetailStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText('详情',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: WisScrollView(
        key: store.scorllViewKey,
        onRefresh: () => store.getDetail(),
        onInit: () => store.getDetail(),
        slivers: <Widget>[
          // buildTop(),
          _ContentComponent(),
        ],
      ),
    );
  }
}

class _ContentComponent extends StatelessWidget {
  const _ContentComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SassNewsDetailStore>(context);
    String content = (store.detail?.content ?? '')
        .replaceAllMapped(RegExp(r'<img src="/\w+/'), (match) {
      return '<img width="100%" src="${APPConfig.baseURL}';
    });
    return SliverToBoxAdapter(
      child: Wisdom.column(
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(15.pt, 30.pt, 15.pt, 30.pt),
        children: <Widget>[
          Container(
            padding: EdgeInsets.fromLTRB(0, 0, 0, 11.5.pt),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: WisText.c000(store.detail?.title ?? '',
                      size: 23.pt, bold: true),
                ),
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 0, 0, 11.5.pt),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                WisText.c000(
                  store.detail?.publishTime ?? '',
                  size: 16.pt,
                  color: Color(0xFF999999),
                ),
                Row(children: <Widget>[
                  Image.asset(
                    'lib/assets/see.png',
                    width: 19.pt,
                    height: 14.pt,
                    package: 'news_package',
                  ),
                  SizedBox(
                    width: 5.pt,
                  ),
                  WisText.c000(
                    (store.detail?.viewCount == null
                        ? ''
                        : store.detail?.viewCount.toString())!,
                    size: 16.pt,
                    color: Color(0xFF333333),
                  ),
                ])
              ],
            ),
          ),
          Divider(
            height: 30.pt,
          ),
          Container(
              child: HtmlWidget(
            content,
          ))
        ],
      ),
    );
  }
}
