import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import 'package:community_package/src/components/list_item.dart';
import 'package:community_package/src/models/community_list_models.dart';
import '../../view_models/private_community_detail_model.dart';
import '../../view_models/private_community_index.model.dart';

//part 'components/list.dart';
class CommunityDetailPage extends StatelessWidget {
  const CommunityDetailPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (providerContext) =>
              PrivateCommunityIndexStore(context, providerContext),
          lazy: false,
        ),
        ChangeNotifierProvider(
          create: (providerContext) =>
              PrivateCommunityDetailStore(context, providerContext),
          child: _PageView(),
          lazy: false,
        ),
      ],
      child: _PageView(),
    );
  }
}

class _PageView extends StatelessWidget {
  const _PageView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateCommunityDetailStore>(context);
    final args = store.routingArgs;
    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('${args['name'] != null ? args['name'] + '详情' : '小区详情'}',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: WisScrollView(
        key: store.scorllViewListKey,
        onRefresh: () => store.getDetail(),
        onInit: () => store.getDetail(),
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: ListItem(
                item: CommunityListModel.fromJson(args), isDetail: true),
          ),
          _ContentComponent(),
        ],
      ),
    );
  }
}

/// 小区详情
class _ContentComponent extends StatelessWidget {
  const _ContentComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final store = Provider.of<PrivateCommunityDetailStore>(context);
    String communityContent = (store.communityDetail?.summary ?? '')
        .replaceAllMapped(RegExp(r'<img src="/\w+/'), (match) {
      return '<img width="100%" src="${APPConfig.baseURL}';
    });
    return SliverToBoxAdapter(
      child: WisCard(
        margin: EdgeInsets.only(
            top: 10.pt, bottom: 10.pt, left: 15.pt, right: 15.pt),
        color: Colors.white,
        child: Wisdom.column(
          padding: EdgeInsets.only(left: 15.pt, right: 15.pt, bottom: 15.pt),
          mainAxisSize: MainAxisSize.min,
          color: Color(0xffffffff),
          children: [
            Wisdom(
              padding: EdgeInsets.only(top: 20.pt),
              alignment: Alignment.centerLeft,
              child: Text(
                '小区介绍',
                style: TextStyle(
                  color: colorScheme.mainTitle,
                  fontWeight: FontWeight.w600,
                  fontSize: 15.pt,
                ),
              ),
            ),
            HtmlWidget(
              communityContent,
            ),
          ],
        ),
      ),
    );
  }
}
