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

import '../../assets.gen.dart';
import '../../view_models/detail_store.dart';

//part 'components/list.dart';
class DynamicDetailPage extends StatelessWidget {
  const DynamicDetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return _PageView();
  }
}

class _PageView extends StatefulWidget {
  const _PageView({Key? key}) : super(key: key);

  @override
  State<_PageView> createState() => __PageViewState();
}

class __PageViewState extends State<_PageView> {
  @override
  void dispose() {
    Get.delete<DetailStore>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final DetailStore store =
        Get.isRegistered() ? Get.find() : Get.put(DetailStore());

    return Scaffold(
      appBar: WAppBar(
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        title: WisText(
          '服务详情',
          size: 19.pt,
          weight: FontWeight.w600,
          color: colorScheme.mainTitle,
        ),
      ),
      body: WisScrollView(
        key: store.scrollViewKey,
        onInit: () => store.getDetail(),
        slivers: [
          Wisdom.row(
            padding: EdgeInsets.all(15.pt),
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Wisdom.row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Obx(() {
                    return Wisdom(
                      margin: EdgeInsets.only(right: 5.pt),
                      child: WImage(
                        image: CachedNetworkImageProvider(
                            AppParseUtil.image(store.item.value.imgUrl!)),
                        height: 25.pt,
                        width: 25.pt,
                        borderRadius: BorderRadius.circular(4.pt),
                        fit: BoxFit.cover,
                      ),
                    );
                  }),
                  Obx(() {
                    return WisText('${store.item.value.publishByName ?? ''}',
                        color: colorScheme.primary);
                  }),
                  SizedBox(width: 5.pt),
                  Obx(() {
                    return WTag(
                      color: store.item.value.type == 'serviceBulletin'
                          ? colorScheme.primary
                          : colorScheme.green,
                      child: WisText(
                        store.item.value.type == 'serviceBulletin'
                            ? '服务简报'
                            : '服务详报',
                        size: 13.pt,
                      ),
                    );
                  }),
                ],
              ),
              Wisdom.row(
                children: [
                  ImageIcon(
                    AssetList.$praise_png_image,
                    size: 13.pt,
                  ),
                  Obx(() {
                    return Text(' ${store.item.value.likes ?? 0}');
                  })
                ],
              ),
            ],
          ).asSliver(),
          Wisdom.row(
            padding: EdgeInsets.fromLTRB(15.pt, 0, 15.pt, 15.pt),
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Obx(() {
                return WisText('类型：${store.className}').asFlexible(flex: 1);
              }),
              Obx(() {
                return WisText(
                  '${store.detail.value.publishAt ?? ''}',
                  color: colorScheme.grey,
                ).asFlexible(flex: 1);
              }),
            ],
          ).asSliver(),
          Divider().asSliver(),
          Wisdom.column(
            padding: EdgeInsets.symmetric(
              horizontal: 15.pt,
              vertical: 15.pt,
            ),
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Obx(() {
                return Wisdom.row(
                  children: [
                    if (store.detail.value.type == "serviceDetailedReport") ...[
                      HtmlWidget(
                        store.content.value,
                      ).asExpanded()
                    ] else ...[
                      WisText(
                        '${store.detail.value.title}',
                        height: 1.5,
                      ).asExpanded()
                    ],
                  ],
                );
              }),
              Obx(() {
                return Wisdom.wrap(
                  margin: EdgeInsets.only(
                      top: store.photosProvider.length > 0 ? 15.pt : 0),
                  crossAxisAlignment: WrapCrossAlignment.start,
                  runSpacing: 10.pt,
                  spacing: 10.pt,
                  children: [
                    for (var i = 0; i < store.photosProvider.length; i++)
                      Obx(() {
                        return Wisdom(
                          onTap: () =>
                              WisPhotoViewUtil.show(store.photosProvider, i),
                          child: WImage(
                            image: store.photosProvider[i],
                            height:
                                (MediaQuery.of(context).size.width - 51.pt) / 3,
                            width:
                                (MediaQuery.of(context).size.width - 51.pt) / 3,
                            borderRadius: BorderRadius.circular(4.pt),
                            fit: BoxFit.cover,
                          ),
                        );
                      })
                  ],
                );
              })
            ],
          ).asSliver(),
          Divider().asSliver(),
          Wisdom.column(
            padding: EdgeInsets.all(15.pt),
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Obx(() {
                return ListView(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  children: <Widget>[
                    if (store.detail.value.files != null &&
                        store.detail.value.files!.length > 0) ...[
                      WisText('附件：'),
                      for (int i = 0; i < store.detail.value.files!.length; i++)
                        _FileItem(itemData: store.detail.value.files![i]),
                    ]
                  ],
                );
              })
            ],
          ).asSliver(),
        ],
      ),
    );
  }
}

class _FileItem extends StatelessWidget {
  const _FileItem({Key? key, this.itemData}) : super(key: key);

  final itemData;

  @override
  Widget build(BuildContext context) {
    final DetailStore store =
        Get.isRegistered() ? Get.find() : Get.put(DetailStore());
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return Wisdom.row(
      margin: EdgeInsets.only(top: 10.pt),
      onTap: () => store.viewFile(itemData),
      // .pdf, .doc, .docx, .xls, .xlsx, .pptx, .ppt, image/*
      children: [
        WisText(
          itemData?.fileName,
          size: 14.pt,
          // maxLines: 1,
          color: colorScheme.primary,
          // overflow: TextOverflow.ellipsis,
        ).asExpanded(),
      ],
    );
  }
}
