import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
import 'package:flutterappfuyou/code/version2/YSUrlPage.dart';

import 'base/YSBase.dart';

class YSCollection extends StatefulWidget {
  @override
  _YSCollectionState createState() => _YSCollectionState();
}

class _YSCollectionState extends State<YSCollection> {
  List _dataArray = [];
  ScrollController _scroll = ScrollController();
  int index;
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
      _refreshData();
      _scroll.addListener(() {
        if(_scroll.position.pixels == _scroll.position.maxScrollExtent){
          this._loadMoreData();
        }
      });
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '收藏',
      yschild: Container(
        color: Colors.white,
        height: MediaQuery.of(context).size.height-125,
        child: RefreshIndicator(
          onRefresh: _refreshData,
          child: _dataArray.length==0?Container(
            alignment: Alignment.center,
            child: Image.asset('lib/images/none.png',height: 200,width: 200,),
          ):ListView.separated(
            itemBuilder: (context,index){
              Map item = _dataArray[index];
              return GestureDetector(
                onTap: (){
                  Navigator.of(context,rootNavigator: true).push(
                      CupertinoPageRoute(builder: (context){
                        return YSUrlPage(url: item['preview_url'],title: item['title'],wiki: item,isCollection: item['is_favorite']??false,);
                      })
                  ).then((value) {
                    _refreshData();
                  });
                },
                behavior: HitTestBehavior.opaque,
                child: item['covers'].length==1?Container(
                  padding: EdgeInsets.all(10),
                  child: Row(
                    children: [
                      Container(
                        height: 70,
                        width: 100,
                        decoration: BoxDecoration(
                            color: Color(0xFFF7CCC8),
                            image: DecorationImage(
                                fit: BoxFit.fill,
                                image: NetworkImage('${_dataArray[index]['covers'][0]}')
                            )
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 10),
                        height: 70,
                        width: MediaQuery.of(context).size.width-140,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('${_dataArray[index]['title']}',style: TextStyle(color: Color(0xFF0F0F0F),fontSize: 14,decoration: TextDecoration.none,fontWeight: FontWeight.w600),
                              maxLines: 1,overflow: TextOverflow.ellipsis,textAlign: TextAlign.left,),
                            Container(
                              margin: EdgeInsets.only(top: 5,bottom: 10),
                              child: Text(_dataArray[index]['contributor']??'',style: TextStyle(color: Color(0xFF0F0F0F),fontSize: 11,
                                  decoration: TextDecoration.none,fontWeight: FontWeight.normal),maxLines: 2,overflow: TextOverflow.ellipsis,),
                            ),
                            Text('${_dataArray[index]['created_at']}',style: TextStyle(color: Color(0xFF0F0F0F),fontSize: 11,
                                decoration: TextDecoration.none,fontWeight: FontWeight.normal),maxLines: 2,overflow: TextOverflow.ellipsis,),
                          ],
                        ),
                      )
                    ],
                  ),
                ):Container(
                  padding: EdgeInsets.all(10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('${_dataArray[index]['title']}',style: TextStyle(color: Color(0xFF0F0F0F),fontSize: 14,decoration: TextDecoration.none,fontWeight: FontWeight.w600),
                        maxLines: 1,overflow: TextOverflow.ellipsis,textAlign: TextAlign.left,),
                      Container(
                        margin: EdgeInsets.only(top: 5,bottom: 5),
                        child: Text('${_dataArray[index]['introduction']}',style: TextStyle(color: Color(0xFF0F0F0F),fontSize: 11,
                            decoration: TextDecoration.none,fontWeight: FontWeight.normal),maxLines: 2,overflow: TextOverflow.ellipsis,),
                      ),
                      Row(
                          children: [
                            for(int i = 0;i<_dataArray[index]['covers'].length;i++)Container(
                              height: 70,
                              margin: EdgeInsets.only(right: 10),
                              width: (MediaQuery.of(context).size.width-50)/3,
                              decoration: BoxDecoration(
                                  color: Color(0xFFF7CCC8),
                                  image: DecorationImage(
                                      fit: BoxFit.fill,
                                      image: NetworkImage('${_dataArray[index]['covers'][i]}')
                                  )
                              ),
                            )
                          ]
                      )
                    ],
                  ),
                ),
              );
            },
            separatorBuilder: (context,index){
              return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFE6E1E1),);
            },
            itemCount: _dataArray.length,
            physics: AlwaysScrollableScrollPhysics(),
            padding: EdgeInsets.all(0),
            controller: _scroll,
          ),
        ),
      ),
    );
  }

  Future<void> _refreshData() async{
    index = 1;
    Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'wike/favoriteList', {'page':index});
    if(dict!=null){
      setState(() {
        _dataArray = dict['data'];
      });
    }
  }
  Future<void> _loadMoreData() async{
    index++;
    Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'wike/favoriteList', {'page':index});
    if(dict!=null){
      setState(() {
        _dataArray.addAll(dict['data']);
      });
    }
  }
}
