import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.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/wode/wode_router.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/fast_notification.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

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

  // final String index;

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

class MyVideoState extends BasePageState<MyVideo, MyVideoListPresenter>
    with AutomaticKeepAliveClientMixin {
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  ScrollController _scrollController = new ScrollController();

  int _page = 1;
  String searchWord;
  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    print(66666);

    FastNotification.addListener("initMyVideo", (initThisMyVideo) {
      if (mounted) {
        _onRefresh();
      }
    });
  }

  getCheckFlag(checkFlag) {
    String str;
    if (checkFlag == 0) {
      str = "已驳回";
    } else if (checkFlag == 1) {
      str = "审核通过";
    } else if (checkFlag == 2) {
      str = "待审核";
    }
    return str;
  }

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

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;

    return ChangeNotifierProvider<BaseListProvider<Records>>(
        create: (_) => provider,
        child: Scaffold(
            appBar: MyAppBar(
              centerTitle: '我的视频',
              // searchWidth: width * 0.7,
              // onPressed: (text) {
              //   searchWord = text;
              //   _onRefresh();
              // },
              actions: <Widget>[
                FlatButton(
                  child: Text(
                    "上传",
                    style: TextStyle(color: Colours.text, fontSize: 14),
                  ),
                  // textColor: Colours.text,
                  // highlightColor: Colors.transparent,
                  onPressed: () {
                    // NavigatorUtils.push(context, BbsRouter.videoUpload);
                    NavigatorUtils.push(
                        context, "${WodeRouter.videoUpload}?id=");
                  },
                )
              ],
            ),
            body: Container(
                child: Stack(
              children: <Widget>[
                Column(
                  children: <Widget>[
                    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: 15,
                                        // right: ScreenUtil().setWidth(15),
                                        top: 10,
                                        bottom: 10),
                                    decoration: BoxDecoration(
                                      border: Border(
                                        bottom: BorderSide(
                                            width: 0.5, color: Colours.line),
                                      ),
                                    ),
                                    child: Row(
                                      // crossAxisAlignment:
                                      //     CrossAxisAlignment.start,
                                      // mainAxisAlignment:
                                      //     MainAxisAlignment.spaceBetween,
                                      children: <Widget>[
                                        Expanded(
                                          // color: Colors.red,
                                          // 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.w400,
                                                        color:
                                                            Color(0xff222222),
                                                        fontSize: 15),
                                                    textAlign: TextAlign.start,
                                                    maxLines: 1,
                                                    overflow:
                                                        TextOverflow.ellipsis,
                                                  ),
                                                ),
                                                Container(
                                                  // width: width * 0.5,
                                                  child: Text(
                                                    "${provider.list[index].descr ?? ""}",
                                                    style: TextStyle(
                                                        fontWeight:
                                                            FontWeight.w600,
                                                        color:
                                                            Color(0xff999999),
                                                        fontSize: 12),
                                                    textAlign: TextAlign.start,
                                                    maxLines: 1,
                                                    overflow:
                                                        TextOverflow.ellipsis,
                                                  ),
                                                ),
                                                SizedBox(
                                                  height: 30,
                                                ),
                                                // Expanded(child: Container()),
                                                Row(
                                                  // crossAxisAlignment: CrossAxisAlignment.baseline,
                                                  mainAxisAlignment:
                                                      MainAxisAlignment
                                                          .spaceBetween,
                                                  children: <Widget>[
                                                    Container(
                                                      alignment:
                                                          Alignment.center,
                                                      padding:
                                                          EdgeInsets.fromLTRB(
                                                              5, 0, 5, 2),
                                                      // width: width * 0.35,
                                                      decoration:
                                                          new BoxDecoration(
                                                        //背景
                                                        color:
                                                            Color(0x215887FF),
                                                        //设置四周圆角 角度
                                                        borderRadius:
                                                            BorderRadius.all(
                                                                Radius.circular(
                                                                    4.0)),
                                                        //设置四周边框
                                                      ),
                                                      child: Text(
                                                        "${provider.list[index].brandName ?? ""}",
                                                        style: TextStyle(
                                                            color: Color(
                                                                0xff999999),
                                                            fontSize: 11),
                                                        textAlign:
                                                            TextAlign.start,
                                                        maxLines: 1,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                      ),
                                                    ),
                                                    // SizedBox(width: 5),
                                                    // Expanded(child: Container()),
                                                    Row(
                                                      // mainAxisAlignment:
                                                      //     MainAxisAlignment
                                                      //         .spaceBetween,
                                                      children: <Widget>[
                                                        Container(
                                                          // width: width * 0.15,
                                                          child: Text(
                                                            "${provider.list[index].browseNum ?? "0"}浏览·${provider.list[index].likeNum ?? "0"}赞",
                                                            style: TextStyle(
                                                                color: Color(
                                                                    0xffCCCCCC),
                                                                fontSize: 11),
                                                            textAlign:
                                                                TextAlign.right,
                                                            maxLines: 1,
                                                            overflow:
                                                                TextOverflow
                                                                    .ellipsis,
                                                          ),
                                                        ),
                                                        SizedBox(
                                                          width: 10,
                                                        )
                                                      ],
                                                    ),
                                                  ],
                                                )
                                              ]),
                                        ),
                                        // Expanded(child: Container()),
                                        Container(
                                            padding: EdgeInsets.only(right: 10),
                                            child: Stack(
                                              children: <Widget>[
                                                ClipRRect(
                                                  borderRadius:
                                                      BorderRadius.circular(7),
                                                  child: Container(
                                                    child: LoadNetworkImage(
                                                      // imgFontUrl +
                                                      //     provider.list[index].cover,
                                                      provider
                                                          .list[index].cover,
                                                      width: 140,
                                                      height: 85,
                                                      isWater: false,
                                                    ),
                                                  ),
                                                ),
                                                Positioned(
                                                  left: width * 0.17,
                                                  top: width * 0.08,
                                                  child: Icon(
                                                    const IconData(0xe8e0,
                                                        fontFamily: "Iconfont"),
                                                    size: 26.0,
                                                    color: Colors.white,
                                                  ),
                                                ),
                                                Positioned(
                                                    right: 0,
                                                    top: 0,
                                                    child: Container(
                                                      padding:
                                                          EdgeInsets.fromLTRB(
                                                              5, 2, 5, 2),
                                                      decoration:
                                                          new BoxDecoration(
                                                        //背景
                                                        color:
                                                            Color(0x4c000000),
                                                        //设置四周圆角 角度
                                                        borderRadius:
                                                            BorderRadius.all(
                                                                Radius.circular(
                                                                    4.0)),
                                                        //设置四周边框
                                                        // border: new Border.all(width: 1, color: Colors.red),
                                                      ),
                                                      child: Text(
                                                        provider.list[index]
                                                                    .checkFlag !=
                                                                null
                                                            ? getCheckFlag(
                                                                provider
                                                                    .list[index]
                                                                    .checkFlag)
                                                            : "",
                                                        style: TextStyle(
                                                            color: Colors.white,
                                                            fontSize: 10),
                                                        textAlign:
                                                            TextAlign.start,
                                                      ),
                                                    ))
                                              ],
                                            )),
                                      ],
                                    )),
                                onTap: () {
                                  NavigatorUtils.push(context,
                                      "${WodeRouter.videoDetail}?id=${provider.list[index].id.toString()}");
                                },
                              );
                            },
                          );
                        }))
                  ],
                ),
              ],
            ))));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getVideoList(_page,
        int.parse(flustars.SpUtil.getString(Constant.userId)), searchWord);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getVideoList(_page,
        int.parse(flustars.SpUtil.getString(Constant.userId)), searchWord);
  }

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

  bool get wantKeepAlive => true;
}
