import 'package:flutter/material.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/internal/bbs/presenter/video_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/utils/url.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/utils/utils.dart';
import 'package:liftmanager/utils/theme_utils.dart';

class VideoList extends StatefulWidget {
  // QuestionList(this.index);

  // final String index;

  @override
  VideoListState createState() => VideoListState();
  // State<StatefulWidget> createState() {
  //   return VideoListState();
  // }
}

class VideoListState
    extends BasePageState<VideoList, VideoListPresenterSeconds> {
// class VideoListState extends State<VideoList> {
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int _page = 1;
  int brandIdss;
  int exprtyIdss;
  bool sortBool = true;
  String checkTypeName;
  String checkType;
  String brandName = "品牌";
  String masterName = "专家";
  ScrollController _scrollController = new ScrollController();
  String searchWord;
  List<dynamic> typeList;
  List<dynamic> brandList;
  Future getBrandList() async {
    await NewApiService().getBrandListNoPage(
        onSuccess: (res) {
      if (res != null) {
        brandList = res;
        typeList = brandList;
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }
  List<dynamic> expertList;
  Future getExpertList() async {
    await NewApiService().getExpertListNoPage(
        onSuccess: (res) {
      if (res != null) {
        expertList = res;
        typeList = expertList;
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
  }

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

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return ChangeNotifierProvider<BaseListProvider<Records>>(
        create: (_) => provider,
        child: Scaffold(
            appBar: SearchAppBar2(
              searchWidth:width*0.7,
              onPressed: (text) {
                searchWord = text;
                _onRefresh();
              },
              actions: <Widget>[
                FlatButton(
                  child: Text(
                    "上传",
                    style: TextStyle(
                        color: Colors.white, fontSize: ScreenUtil().setSp(15)),
                  ),
                  textColor: Colours.dark_text,
                  highlightColor: Colors.transparent,
                  onPressed: () {
                    NavigatorUtils.push(context, BbsRouter.videoUpload);
                  },
                )
                // Utils.getAuthByRouter('upload_video', false) ? 
                // FlatButton(
                //   child: Text(
                //     "上传",
                //     style: TextStyle(
                //         color: Colors.white, fontSize: ScreenUtil().setSp(15)),
                //   ),
                //   textColor: Colours.dark_text,
                //   highlightColor: Colors.transparent,
                //   onPressed: () {
                //     NavigatorUtils.push(context, BbsRouter.videoUpload);
                //   },
                // ) 
                // : Container()
              ],
            ),
            body: Container(
              child:Stack(
                children: <Widget>[
                  Column(
              children: <Widget>[
                Container(
                    width: width,
                    height: ScreenUtil().setWidth(50),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(width: 0.5, color: Colours.line),
                      ),
                    ),
                    padding: EdgeInsets.only(
                        left: ScreenUtil().setWidth(15),
                        right: ScreenUtil().setWidth(15)),
                    // color: Color(0xFFF1F4FC),
                    child: Row(
                      children: <Widget>[
                        GestureDetector(
                          onTap: (){
                            typeList = [];
                            getBrandList();
                            setState(() {
                              sortBool = false;
                              checkTypeName = "品牌";
                              checkType = "brand";
                            });
                            
                          },
                          child: Row(
                          children: <Widget>[
                            Text(
                              brandName,
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(16)),
                              textAlign: TextAlign.start,
                            ),
                            Container(
                              padding: EdgeInsets.only(top: 3),
                              child: Icon(
                                Icons.keyboard_arrow_down,
                                size: 26.0,
                              ),
                            )
                          ],
                        ),
                        ),
                        SizedBox(width: ScreenUtil().setWidth(30)),
                        // GestureDetector(
                        //   onTap: (){
                        //     typeList = [];
                        //     getExpertList();
                        //     setState(() {
                        //       sortBool = false;
                        //       checkTypeName = "专家";
                        //       checkType = "master";
                        //     });
                        //   },
                        //   child: Row(
                        //   children: <Widget>[
                        //     Text(
                        //       masterName,
                        //       style: TextStyle(
                        //           color: Color(0xff333333),
                        //           fontSize: ScreenUtil().setSp(16)),
                        //       textAlign: TextAlign.start,
                        //     ),
                        //     Container(
                        //       padding: EdgeInsets.only(top: 3),
                        //       child: Icon(
                        //         Icons.keyboard_arrow_down,
                        //         size: 26.0,
                        //         color: Color(0xff333333),
                        //       ),
                        //     )
                        //   ],
                        // ),
                        // ),
                      ],
                    )),
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('video_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        pageSize:10,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return GestureDetector(
                            child: Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  // right: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setHeight(10),
                                  bottom: ScreenUtil().setHeight(10)),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: Colours.line),
                                ),
                              ),
                              child: 
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.only(top:ScreenUtil().setWidth(8),bottom:ScreenUtil().setWidth(8)),
                                    height: width*0.24,
                                    child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Container(
                                      width:width*0.5,
                                      child: Text(
                                      "${provider.list[index].title??""}",
                                      style: TextStyle(
                                          fontWeight: FontWeight.w600,
                                          fontSize: ScreenUtil().setSp(17)),
                                      textAlign: TextAlign.start,
                                      maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                    ),
                                    ),
                                    Container(
                                      width:width*0.5,
                                      child: Text(
                                      "${provider.list[index].descr??""}",
                                      style: TextStyle(
                                          fontSize: ScreenUtil().setSp(15)),
                                      textAlign: TextAlign.start,
                                      maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                    ),
                                    ),
                                    Container(
                                      width: width*0.5,
                                      child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        Container(
                                          width:width*0.18,
                                          child: Text(
                                            "${provider.list[index].brandName??""}",
                                            style: TextStyle(
                                                color: Color(0xff999999),
                                                fontSize: ScreenUtil().setSp(14)),
                                            textAlign: TextAlign.start,
                                            maxLines: 1,
                                                overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                        SizedBox(
                                          width:5
                                        ),
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(
                                              "${provider.list[index].likeNum??"0"}",
                                              style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: ScreenUtil().setSp(14)),
                                              textAlign: TextAlign.start,
                                            ),
                                            SizedBox(
                                              width:3
                                            ),
                                            Icon(
                                              IconData(0xe7cd,
                                                  fontFamily: "myfont"),
                                              size: 14.0,
                                              color: Color(0xff999999),
                                            ),
                                          ],
                                        ),
                                        
                                        SizedBox(
                                          width:5
                                        ),
                                        Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(
                                              "${provider.list[index].browseNum??"0"}",
                                              style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: ScreenUtil().setSp(14)),
                                              textAlign: TextAlign.start,
                                            ),
                                            SizedBox(
                                              width:3
                                            ),
                                            Icon(
                                              IconData(0xe610,
                                                  fontFamily: "myfont"),
                                              size: 14.0,
                                              color: Color(0xff999999),
                                            ),
                                          ],
                                        ),
                                        
                                        
                                      ],
                                    ),
                                    )
                                  ]),
                                  ),
                                  Container(
                                    padding: EdgeInsets.only(right:10),
                                        child: Stack(
                                      children: <Widget>[
                                        ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(1),
                                          child: Container(
                                            child: LoadNetworkImage(
                                              // imgFontUrl +
                                              //     provider.list[index].cover,
                                              provider.list[index].cover,
                                              width: width*0.38,
                                              height: width*0.24,
                                              isWater: true,
                                            ),
                                          ),
                                        ),
                                        Positioned(
                                          left: width*0.17,
                                          top: width*0.08,
                                          child: Icon(
                                            IconData(0xe607,
                                                fontFamily: "myfont"),
                                            size: 26.0,
                                            color: Colors.white,
                                          ),
                                        )
                                      ],
                                    )),
                                ],
                              )
                              
                            ),
                            onTap: () {
                              NavigatorUtils.push(context,
                                "${BbsRouter.videoDetail}?id=${provider.list[index].id.toString()}");
                            },
                          );
                        },
                      );
                    }))
              ],
            ),
            !sortBool?
            Positioned(
              top:0,
              left:0,
              child: GestureDetector(
                onTap: (){
                  setState(() {
                    sortBool = true;
                  });
                },
                child: Container(
                width:width,
                height:height,
                color: Color.fromRGBO(0, 0, 0, 0.5)
              ),
              )
            )
            :Container(
              child:null
            ),
            !sortBool?
            Positioned(
              top:0,
              left:0,
              child:Container(
                width: width,
                height:height/2,
                color: ThemeUtils.getDialogTextFieldColor(context),
                padding: EdgeInsets.all(10),
                child:ListView(
                  children:<Widget>[
                    GestureDetector(
                      onTap: (){
                        if(checkType=="brand"){
                          
                          setState(() {
                            brandIdss = null;
                            brandName = '品牌';
                            sortBool = true;
                            _onRefresh();
                          });
                        }else if (checkType=="master"){
                          
                          setState(() {
                            exprtyIdss = null;
                            masterName = '专家';
                            sortBool = true;
                            _onRefresh();
                          });
                        }
                        
                        
                      },
                      child:Container(
                      padding: EdgeInsets.only(left:5,top:5,bottom:10),
                      child:Text(
                                  checkTypeName,
                                  style: TextStyle(
                                    color:Color(0xff666666),
                                    fontSize:ScreenUtil().setSp(16)
                                  ),
                                  textAlign:TextAlign.start,
                                ),
                    )
                    ),
                   typeList!=null&&typeList!=[]?Container(
                     child: Wrap(
                  alignment: WrapAlignment.start,
                  crossAxisAlignment: WrapCrossAlignment.center,
                  children: typeList.asMap().keys.map((index){
                    return InkWell(
                      onTap: (){
                            print("666");
                            
                            setState(() {
                              sortBool = true;
                              if(checkType=="brand"){
                                brandName = typeList[index].name??"";
                                brandIdss = typeList[index].id;
                                provider.setStateTypeNotNotify(StateType.loading);
                                _onRefresh();
                              }else if(checkType=="master"){
                                masterName= typeList[index].name??'';
                                exprtyIdss= typeList[index].userId;
                                provider.setStateTypeNotNotify(StateType.loading);
                                _onRefresh();
                              }
                            });
                            
                          },
                      child: Container(
                      width: width/4-15,
                      padding: EdgeInsets.only(bottom:ScreenUtil().setWidth(10),top:ScreenUtil().setWidth(10)),
                      margin: EdgeInsets.only(left:5,right:5,bottom:5,top:5),
                      decoration: BoxDecoration(
                        // border: Border(
                        //   bottom: BorderSide(width: 0.5, color: Colours.line),
                        // ),
                        color: Color(0xfff5f5f5)
                      ),
                      child:Text(
                                  typeList[index].name??"",
                                  style: TextStyle(
                                    color: typeList[index].id == brandIdss?Color(0xffff0000):Color(0xff666666),
                                    fontSize:ScreenUtil().setSp(15)
                                  ),
                                  textAlign:TextAlign.center,
                                ),
                    ),
                    );
                    
                  }).toList(),
                )
                   ):loadCircle()
                  ],
                  
                )
              )
            ):
            Container(
              child:null
            ),
                ],
              )
            )
            )
            );
  }

  Widget loadCircle() {
    return Container(
      padding: EdgeInsets.only(top: 10, bottom: 10),
      color: ThemeUtils.getTabsBg(context),
      child: Center(
        child: SpinKitFadingCircle(
          color: Colors.blueAccent,
          size: 30.0,
        ),
      ),
    );
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getVideoList(_page,brandIdss,searchWord);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getVideoList(_page,brandIdss,searchWord);
  }

  @override
  VideoListPresenterSeconds createPresenter() {
    return VideoListPresenterSeconds();
  }
}
