import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/ui_kit/theme/socialchat_theme.dart';
import '../../../../app_localizations.dart';
import '../../../../shared/tools/sc_banner_utils.dart';
import '../../../../shared/data_sources/models/enum/sc_banner_type.dart';
import '../../../../shared/data_sources/sources/repositories/sc_config_repository_imp.dart';
import '../../../../shared/business_logic/models/res/sc_index_banner_res.dart';
import '../../../../ui_kit/components/sc_compontent.dart';

class HomeEventPage extends StatefulWidget {
  @override
  _HomeEventPageState createState() => _HomeEventPageState();
}

class _HomeEventPageState extends State<HomeEventPage>
    with SingleTickerProviderStateMixin {
  bool isLoading = false;
  List<SCIndexBannerRes> homeBanners = [];
  final RefreshController _refreshController = RefreshController(
    initialRefresh: false,
  );

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.transparent,
      body: Column(
        children: [
          SizedBox(height: ScreenUtil().statusBarHeight + 10.w),
          Row(
            children: [
              SizedBox(width: 15.w),
              text(
                SCAppLocalizations.of(context)!.explore,
                textColor: SocialChatTheme.primaryLight,
                fontSize: 18.sp,
                fontStyle: FontStyle.italic,
                fontWeight: FontWeight.bold,
              ),
            ],
          ),
          SizedBox(height: 8.w,),
          Row(
            children: [
              SizedBox(width: 20.w),
              text(
                SCAppLocalizations.of(context)!.popularEvents,
                textColor: SocialChatTheme.primaryLight,
                fontSize: 14.sp,
              ),
            ],
          ),
          Expanded(
            child: SmartRefresher(
              enablePullDown: true,
              enablePullUp: false,
              controller: _refreshController,
              onRefresh: () {
                _loadData();
              },
              onLoading: () {},
              child:
                  homeBanners.isNotEmpty
                      ? SingleChildScrollView(
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [_banner()],
                        ),
                      )
                      : GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          _loadData();
                        },
                        child:
                            isLoading
                                ? Center(
                                  child: CupertinoActivityIndicator(
                                    color: Colors.white24,
                                  ),
                                )
                                : mainEmpty(
                                  msg: SCAppLocalizations.of(context)!.noData,
                                ),
                      ),
            ),
          ),
        ],
      ),
    );
  }

  _banner() {
    return homeBanners.isNotEmpty
        ? Container(
          margin: EdgeInsets.symmetric(horizontal: 10.w),
          child: ListView.separated(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: homeBanners.length,
            // 列表项数量
            padding: EdgeInsets.only(bottom: 15.w),
            itemBuilder: (context, index) {
              return GestureDetector(
                onTap: () {
                  print('ads:${homeBanners[index].toJson()}');
                  SCBannerUtils.openBanner(homeBanners[index], context);
                },
                child: netImage(
                  height: 118.w,
                  url: homeBanners[index].cover ?? "",
                  fit: BoxFit.fill,
                  borderRadius: BorderRadius.all(Radius.circular(12.w)),
                ),
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return SizedBox(height: 10.w);
            },
          ),
        )
        : Container();
  }

  _loadData() async {
    setState(() {
      isLoading = true;
    });
    try {
      var banners = await SCConfigRepositoryImp().getBanner();
      homeBanners.clear();
      for (var v in banners) {
        if ((v.displayPosition ?? "").contains(SCBannerType.HOME_ALERT.name)) {
          homeBanners.add(v);
        }
      }
      _refreshController.refreshCompleted();
      _refreshController.loadComplete();
      isLoading = false;
      if (mounted) setState(() {});
    } catch (e) {
      _refreshController.refreshCompleted();
      _refreshController.loadComplete();
      isLoading = false;
      if (mounted) setState(() {});
    }
  }
}
