/*
 * @Author: LiOS
 * @Date: 2020-05-14 11:42:05
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2022-10-21 14:51:09
 * @Description: 通知详情页面，如果是草稿则会有重新编辑和确认发布按钮
 */
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../../src/view_models/notice_detail_store.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class DetailPage extends StatelessWidget {
  const DetailPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _PageView();
  }
}

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 = NoticeDetailStore();
    var _notice = _store.notice.value;
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: WAppBar(
        brightness: Brightness.light,
        title: WisText(_notice.title ?? '通知详情',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
      ),
      body: _BuildContent(),
      bottomNavigationBar: _BuildButtons(),
    );
  }
}

class _BuildButtons extends GetView<NoticeDetailStore> {
  const _BuildButtons({Key? key}) : super(key: key);
  static const double BOTTOM_NAVIGATION_BAR_HEIGHT = 44.0;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: Get.arguments['status'] != null &&
              Get.arguments['status'] == 'statusDraft'
          ? BOTTOM_NAVIGATION_BAR_HEIGHT
          : 0,
      margin: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 5.pt),
      child: Column(
        children: <Widget>[
          Expanded(
            child: _BuildBottomButtons(),
          ),
        ],
      ),
    );
  }
}

class _BuildContent extends GetView<NoticeDetailStore> {
  const _BuildContent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _store = NoticeDetailStore();
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    Widget buildNoticeType(String type, String title) {
      if (type == 'importance') {
        return _BuildTypeContainer(
          text: '重要通知',
          title: title,
          textColor: Color.fromRGBO(253, 144, 37, 1),
        );
      } else if (type == 'prompt') {
        return _BuildTypeContainer(
          text: '温馨提示',
          title: title,
          textColor: Color.fromRGBO(37, 180, 30, 1),
        );
      } else if (type == 'urgency') {
        return _BuildTypeContainer(
          text: '紧急通知',
          title: title,
          textColor: Color.fromRGBO(249, 87, 89, 1),
        );
      } else if (type == 'PropertyPublicity') {
        return _BuildTypeContainer(
            text: '物业公示',
            title: title,
            textColor: Color.fromRGBO(37, 180, 30, 1));
      } else if (type == 'propertyCommittee') {
        return _BuildTypeContainer(
            text: '业委会公示',
            title: title,
            textColor: Color.fromRGBO(37, 180, 30, 1));
      } else {
        return _BuildTypeContainer(
          text: '普通通知',
          title: title,
          textColor: Color.fromRGBO(52, 142, 237, 1),
        );
      }
    }

    return WisScrollView(
      onRefresh: () => _store.getDetail(),
      onInit: () => _store.getDetail(),
      onLoad: () => _store.getDetail(),
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: Wisdom.column(
            padding: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 15.pt),
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 5.pt),
                  child: Obx(
                    () => buildNoticeType(
                      _store.notice.value.type ?? '',
                      _store.notice.value.title ?? '',
                    ),
                  )),
              Wisdom(
                margin: EdgeInsets.only(bottom: 10.pt),
                child: Obx(
                  () => (Text.rich(
                    TextSpan(children: [
                      WidgetSpan(
                        child: Text(
                          "主题词: ",
                          style: TextStyle(
                            fontWeight: FontWeight.w300,
                            fontSize: 15.pt,
                            color: colorScheme.minor,
                          ),
                        ),
                      ),
                      TextSpan(
                        text: _store.notice.value.theme ?? '--',
                        style: TextStyle(
                          fontSize: 15.pt,
                          color: colorScheme.minor,
                        ),
                      ),
                    ]),
                    maxLines: 2,
                  )),
                ),
              ),
              Wisdom.row(
                children: [
                  Wisdom(
                    margin: EdgeInsets.only(right: 10.pt),
                    child: Text(
                      "发布时间: ",
                      style: TextStyle(
                        fontWeight: FontWeight.w300,
                        fontSize: 14.pt,
                        color: colorScheme.minor,
                      ),
                    ),
                  ),
                  Wisdom(
                    child: Obx(
                      () => WisText(
                        _store.notice.value.publishAt ?? '--',
                        size: 14.pt,
                        color: colorScheme.minor,
                      ),
                    ),
                  ).asFlexible(flex: 1),
                ],
              ),
              Divider(
                height: 25.pt,
              ),
              Container(
                child: Obx(
                  () => HtmlWidget(
                    _store.content.value,
                    textStyle: TextStyle(fontSize: 16.pt),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class _BuildTypeContainer extends GetView<NoticeDetailStore> {
  const _BuildTypeContainer({
    Key? key,
    this.textColor,
    this.text,
    this.title,
  }) : super(key: key);
  final textColor;
  final text;
  final title;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(0, 0, 0, 10.pt),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Text.rich(
            TextSpan(children: [
              WidgetSpan(
                child: WTag(
                  color: textColor,
                  child: Text(
                    text,
                  ),
                ),
              ),
              TextSpan(
                text: title,
                style: TextStyle(
                  fontSize: 16.pt,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ]),
            maxLines: 100,
          ),
        ],
      ),
    );
  }
}

class _BuildBottomButtons extends GetView<NoticeDetailStore> {
  const _BuildBottomButtons({Key? key}) : super(key: key);
  // static const double TITLE_FONT_SIZE = 17.0;

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final _store = NoticeDetailStore();

    publishConfirm(arg) async {
      Get.dialog(
        Center(
          child: Container(
            height: 153.pt,
            width: 307.pt,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(3.pt),
            ),
            child: Column(
              children: [
                Center(
                  child: Container(
                    padding: EdgeInsets.only(top: 30.pt, bottom: 36.pt),
                    child: Text(
                      '确认发布该公告吗？',
                      style: TextStyle(
                          fontSize: 18.pt,
                          color: Colors.black,
                          fontWeight: FontWeight.normal,
                          decoration: TextDecoration.none),
                    ),
                  ),
                ),
                Container(
                  alignment: Alignment.bottomCenter,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        height: 44.pt,
                        width: 126.pt,
                        child: WButton(
                          color: Color(0xFFF2F2F2),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20.pt),
                          ),
                          child: Text(
                            '取消',
                            style: TextStyle(
                                fontSize: 16.pt, color: Color(0xFF222222)),
                          ),
                          onPressed: () => _store.routeBack(),
                        ),
                      ),
                      SizedBox(
                        width: 15.pt,
                      ),
                      Container(
                        height: 44.pt,
                        width: 126.pt,
                        child:
                            //  WButton.loading(
                            WButton(
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20.pt),
                          ),
                          color: colorScheme.primary,
                          child: Text(
                            '确认',
                            style: TextStyle(fontSize: 16.pt),
                          ),
                          onPressed: () {
                            _store.publishNotice(arg['id']);
                          },
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
        routeSettings: RouteSettings(arguments: arg),
      );
    }

    return Wisdom.row(
      children: <Widget>[
        WButton(
          onPressed: () => {
            Navigator.pop(context, {'published': false})
          },
          color: Colors.transparent,
          shape: RoundedRectangleBorder(
            side: BorderSide(width: 1, color: colorScheme.primary),
            borderRadius: BorderRadius.circular(20.pt),
          ),
          textColor: colorScheme.primary,
          height: 44.pt,
          child: Text('重新编辑'),
        ).asExpanded(),
        SizedBox(width: 15.pt),
        WLoadingButton(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.pt),
          ),
          onPressed: () => publishConfirm(Get.arguments),
          color: colorScheme.primary,
          height: 44.pt,
          child: Text('确认发布'),
        ).asExpanded(),
      ],
    );
  }
}
