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

import '../../base/YSNetWorking.dart';

class YSWikiListView extends StatefulWidget {
  final Map wikiItem;
  final VoidCallback callback;
  const YSWikiListView({Key key, this.wikiItem, this.callback}) : super(key: key);

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

class _YSWikiListViewState extends State<YSWikiListView> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        Navigator.of(context,rootNavigator: true).push(
          CupertinoPageRoute(builder: (context){
            return YSUrlPage(url: widget.wikiItem['preview_url'],title: widget.wikiItem['title'],wiki: widget.wikiItem,isCollection: widget.wikiItem['is_favorite']??false,);
          })
        ).then((value) {
          if(widget.callback!=null){
            widget.callback();
          }
        });
      },
      behavior: HitTestBehavior.opaque,
      child: Container(
        child: widget.wikiItem['covers'].length==1?Container(
          height: 90,
          child: Row(
            children: [
              Container(
                height: 70,
                width: 100,
                decoration: BoxDecoration(
                    color: Color(0xFFF7CCC8),
                    image: DecorationImage(
                        fit: BoxFit.cover,
                        image: NetworkImage('${widget.wikiItem['covers'][0]}')
                    )
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 10),
                // height: 60,
                width: MediaQuery.of(context).size.width-140,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('${widget.wikiItem['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('${widget.wikiItem['contributor']}',style: TextStyle(color: Color(0xFF0F0F0F),fontSize: 11,
                          decoration: TextDecoration.none,fontWeight: FontWeight.normal),maxLines: 2,overflow: TextOverflow.ellipsis,),
                    ),
                    Row(
                      children: [
                        Container(
                          width: MediaQuery.of(context).size.width-240,
                          child: Text('${widget.wikiItem['created_at']}',style: TextStyle(color: Color(0xFF0F0F0F),fontSize: 11,
                              decoration: TextDecoration.none,fontWeight: FontWeight.normal),maxLines: 2,overflow: TextOverflow.ellipsis,),
                        ),
                        Container(
                          alignment: Alignment.center,
                          child: Row(
                            children: [
                              Icon(Icons.remove_red_eye_outlined,size: 10,color: Colors.grey,),
                              Container(
                                padding: EdgeInsets.only(left: 1),
                                width: 40,
                                child: Text('${widget.wikiItem['views']}',style: TextStyle(color: Colors.grey,fontSize: 11,
                                    decoration: TextDecoration.none,fontWeight: FontWeight.normal),maxLines: 1,overflow: TextOverflow.ellipsis,),
                              )
                            ],
                          ),
                        ),
                        Container(
                          alignment: Alignment.center,
                          child: GestureDetector(
                            onTap: () async{
                              Map dict = await ysRequestHttpNoLoading(context, requestType.put, 'wike/like', {'wike_id':widget.wikiItem['id']});
                              if(dict!=null){
                                widget.wikiItem['is_like'] = dict['data']['is_like'];
                                widget.wikiItem['count_like'] = dict['data']['count_like'];
                                setState(() {});
                              }
                            },
                            child: Row(
                              children: [
                                Icon(Icons.favorite,size: 10,color: widget.wikiItem['is_like']==true?Colors.pinkAccent:Colors.grey,),
                                Container(
                                  width: 40,
                                  padding: EdgeInsets.only(left: 1),
                                  child: Text('${widget.wikiItem['count_like']}',style: TextStyle(color: Colors.grey,fontSize: 11,
                                      decoration: TextDecoration.none,fontWeight: FontWeight.normal),maxLines: 1,overflow: TextOverflow.ellipsis,),
                                )
                              ],
                            ),
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              )
            ],
          ),
        ):Container(
          height: 145,
          padding: EdgeInsets.only(top: 5,bottom: 5),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('${widget.wikiItem['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('${widget.wikiItem['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<widget.wikiItem['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('${widget.wikiItem['covers'][i]}')
                          )
                      ),
                    )
                  ]
              )
            ],
          ),
        ),
      ),
    );
  }
}
