import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/article.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/view_widget/classic_header.dart';
import 'package:huixiang/view_widget/hot_item.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/my_footer.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HotArticlePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HotArticlePage();
  }
}

class _HotArticlePage extends State<HotArticlePage> {
  ApiService apiService;

  @override
  void dispose() {
    super.dispose();
    refreshController.dispose();
  }

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

    SharedPreferences.getInstance().then((value) => {
          apiService =  ApiService(Dio(), context: context, token: value.getString("token")),
          queryArticle(),
        });
  }

  int pageNum = 1;
  RefreshController refreshController = RefreshController();

  _onRefresh() {
    pageNum = 1;
    queryArticle();
  }

  List<Article> articles = [];

  queryArticle() async {
    BaseData<PageInfo<Article>> baseData = await apiService.queryArticle({
      "pageNum": pageNum,
      "pageSize": 10,
      "searchKey": "",
      "state": 1,
      "type": 2
    }).catchError((onError){
      refreshController.refreshFailed();
      refreshController.loadFailed();
    });
    if (baseData != null && baseData.isSuccess) {
      refreshController.refreshCompleted();
      refreshController.loadComplete();
      if(pageNum == 1) {
        articles.clear();
      }
      articles.addAll(baseData.data.list);
      if (baseData.data.pageNum == baseData.data.pages) {
        refreshController.loadNoData();
      } else {
        pageNum += 1;
      }
      setState(() {});
    } else {
      refreshController.refreshFailed();
      refreshController.loadFailed();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        background: Color(0xFFF7F7F7),
        leadingColor: Colors.black,
        title: S.of(context).huixiangtoutiao,
        titleColor: Colors.black,
        titleSize: 18.sp,
      ),
      body: Container(
        child: SmartRefresher(
          controller: refreshController,
          enablePullDown: true,
          enablePullUp: true,
          physics: BouncingScrollPhysics(),
          header: MyHeader(),
          footer: CustomFooter(
            builder: (context, mode) {
              return MyFooter(mode);
            },
          ),
          onRefresh: _onRefresh,
          onLoading: queryArticle,
          child: ListView.builder(
            itemCount: articles.length,
            physics: NeverScrollableScrollPhysics(),
            scrollDirection: Axis.vertical,
            itemBuilder: (context, position) {
              return AspectRatio(
                aspectRatio: position == 0 ? 1.38 : 3.56,
                child: Container(
                  height: position == 0 ? 247.h : 96.h,
                  margin: EdgeInsets.symmetric(vertical: 6.h, horizontal: 16.w),
                  child: HotArticleItem(article: articles[position], isHot: position == 0),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}