/*
 * @Author: ChenYaJin
 * @Date: 2023-01-12 14:56:17
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-01-16 10:42:39
 * @Description: 
 */
import 'dart:convert';
import 'package:app_package/app_package.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:task_package/src/view_models/dynamic_store.dart';
import 'package:task_package/src/assets.gen.dart';
import 'package:task_package/src/components/task_item.dart';

class DynamicPage extends StatelessWidget {
  const DynamicPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    return Scaffold(
      appBar: WAppBar(
        title: WisText(
          '任务动态',
          color: colorScheme.mainTitle,
          weight: FontWeight.w600,
          size: 18.pt,
        ),
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
      ),
      body: DynamicView(),
    );
  }
}

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

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

class _DynamicViewState extends State<DynamicView>
    with SingleTickerProviderStateMixin {
  final DynamicStore store = Get.isRegistered<DynamicStore>()
      ? Get.find<DynamicStore>()
      : Get.put(DynamicStore());

  @override
  void dispose() {
    Get.delete<DynamicStore>();
    super.dispose();
  }

  @override
  void initState() {
    store.onInitData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final List? list = store.status;
    final colorScheme = WTheme.of(context).colorScheme;

    return WisScrollView(slivers: <Widget>[
      SliverToBoxAdapter(
          child: Wisdom(
              padding: EdgeInsets.symmetric(horizontal: 15.pt),
              margin: EdgeInsets.only(top: 15.pt),
              child: Obx(() => TaskItem(
                    item: store.detail.value,
                  )))),
      SliverToBoxAdapter(
          child: Obx(() => Wisdom(
              padding: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
              margin: EdgeInsets.only(bottom: 10.pt),
              child: Wisdom.column(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12.pt),
                boxShadow: [
                  BoxShadow(
                    offset: Offset(0, 2),
                    color: Color(0x08000000),
                    blurRadius: 10.pt,
                    spreadRadius: 2.5.pt,
                  ),
                ],
                children: <Widget>[
                  for (var i = 0; i < list!.length; i++)
                    Wisdom.stack(
                      clipBehavior: Clip.none,
                      margin: EdgeInsets.all(15.pt),
                      children: <Widget>[
                        Wisdom.column(
                          margin: EdgeInsets.only(left: 15.pt),
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            Wisdom.row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  WisText(
                                    '${list[i]?.title ?? ''}',
                                    bold: true,
                                    color: colorScheme.minor,
                                    size: 14.pt,
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                  ).asExpanded(),
                                  if (list[i]?.canDelete == true) ...[
                                    Wisdom(
                                      onTap: () => store.deleteState(list[i]),
                                      padding: EdgeInsets.only(
                                        right: 5.pt,
                                        left: 15.pt,
                                      ),
                                      child: Image(
                                        image: AssetList.$delRound_png_image,
                                        height: 16.pt,
                                      ),
                                    ),
                                  ],
                                ]),
                            SizedBox(height: 5.pt),
                            WisText(
                              '${list[i]?.createdAt ?? ''}',
                              color: colorScheme.minor,
                              size: 13.pt,
                              height: 1.5,
                            ),
                            SizedBox(height: 10.pt),
                            if (list[i]?.content != null)
                              if (list[i].type == 'img')
                                Builder(builder: (_) {
                                  List<String> photoList =
                                      list[i]?.content?.split(',')?.toList();
                                  return Wisdom.wrap(
                                      runSpacing: 5.pt,
                                      spacing: 5.pt,
                                      children: <Widget>[
                                        for (var j = 0;
                                            j < photoList.length;
                                            j++)
                                          Wisdom.wrap(
                                              runSpacing: 5.pt,
                                              spacing: 5.pt,
                                              children: [
                                                Wisdom(
                                                    onTap: () =>
                                                        WisPhotoViewUtil.show([
                                                          CachedNetworkImageProvider(
                                                            AppParseUtil.image(
                                                                photoList[j]),
                                                          )
                                                        ], j),
                                                    child: WImage.network(
                                                      AppParseUtil.image(
                                                          photoList[j]),
                                                      fit: BoxFit.cover,
                                                      width: 90.pt,
                                                      height: 90.pt,
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              4.pt),
                                                    ))
                                              ])
                                      ]);
                                })
                              else if (list[i]?.type == 'file')
                                Builder(
                                  builder: (_) {
                                    var content = json.decode(list[i].content);
                                    return Wisdom(
                                      onTap: () => store.route2perview(
                                        APPConfig.baseURL +
                                            'file/download/' +
                                            content['fileId'],
                                        content['name'],
                                      ),
                                      alignment: Alignment.centerLeft,
                                      color: colorScheme.background,
                                      borderRadius: BorderRadius.circular(4.pt),
                                      padding: EdgeInsets.all(15.pt),
                                      child: Text(
                                        '${content['name'] ?? ''}',
                                        style: TextStyle(
                                          fontSize: 13.pt,
                                          color: colorScheme.blue,
                                        ),
                                      ),
                                    );
                                  },
                                )
                              else
                                Wisdom(
                                  alignment: Alignment.centerLeft,
                                  color: colorScheme.background,
                                  borderRadius: BorderRadius.circular(4.pt),
                                  padding: EdgeInsets.all(15.pt),
                                  child: WisText(
                                    '${list[i]?.content ?? ''}',
                                    maxLines: 100,
                                    height: 1.8,
                                    size: 13.pt,
                                    color: colorScheme.minor,
                                  ),
                                ),
                          ],
                        ),
                        if (i != list.length - 1)
                          Positioned(
                            left: 0.pt,
                            top: 5.pt,
                            bottom: -35.pt,
                            child: Container(
                              width: 1,
                              color: colorScheme.divider,
                            ),
                          ),
                        Positioned(
                            left: -5.pt,
                            top: 5.pt,
                            child: Wisdom.row(
                              children: [
                                if (i == 0)
                                  Wisdom(
                                    width: 10.pt,
                                    height: 10.pt,
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(10.pt),
                                    border: Border.all(
                                      width: 1,
                                      color: colorScheme.primary,
                                    ),
                                    boxShadow: [
                                      BoxShadow(
                                        color: colorScheme.primary
                                            .withOpacity(0.3),
                                        blurRadius: 4.pt,
                                        spreadRadius: 2.pt,
                                      ),
                                    ],
                                  )
                                else
                                  Wisdom(
                                    width: 10.pt,
                                    height: 10.pt,
                                    color: colorScheme.divider,
                                    borderRadius: BorderRadius.circular(10.pt),
                                  ),
                              ],
                            )),
                      ],
                    ),
                ],
              ))))
    ]);
  }
}
