import 'package:flutter/material.dart';
import 'package:liftmanager/internal/means/means_router.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:liftmanager/internal/means/page/means_pdf.dart';

import 'package:liftmanager/res/resources.dart';
import 'dart:convert';
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/internal/wode/presenter/notice_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/wode/model/notice_model.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/utils/log_util.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'dart:math';
import 'package:liftmanager/utils/fast_notification.dart';

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

  // final String index;

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

class NoticeListState extends BasePageState<NoticeList, NoticeListPresenter> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();

  int _page = 1;
  @override
  void dispose() {
    super.dispose();
  }

  Future changeNoticeState(idList) async {
    await NewApiService().changeNotificaList(idList, onSuccess: (res) {
      String initThisNotice = randomInt(1111,9999).toString() + DateTime.now().millisecondsSinceEpoch.toString();
      FastNotification.push("initNotice",initThisNotice);
      _onRefresh();
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  randomInt(int min, int max) {
    return new Random().nextInt(max) % (max - min + 1) + min;
  }

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

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

    return ChangeNotifierProvider<BaseListProvider<Records>>(
        create: (_) => provider,
        child: Scaffold(
          // resizeToAvoidBottomPadding: true,
          appBar: MyAppBar(
            centerTitle: "我的消息",
          ),
          body: Container(
            // color: Color(0xFFF1F4FC),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('notice_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        pageSize: 15,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return InkWell(
                            child: Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  right: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setWidth(10),
                                  bottom: ScreenUtil().setWidth(10)),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: Colours.line),
                                ),
                              ),
                              child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Container(
                                      padding: EdgeInsets.only(
                                          right: ScreenUtil().setWidth(10)),
                                      child: ClipRRect(
                                        borderRadius: BorderRadius.circular(
                                            ScreenUtil().setWidth(18)),
                                        child: Container(
                                          width: ScreenUtil().setWidth(36),
                                          height: ScreenUtil().setWidth(36),
                                          decoration: BoxDecoration(
                                            gradient: const LinearGradient(
                                                colors: [
                                                  Color(0xFF00D9FF),
                                                  Color(0xFF0287FF)
                                                ]),
                                          ),
                                          // padding: EdgeInsets.only(right:10),
                                          child: Icon(
                                            IconData(0xe62f,
                                                // 0xe609,
                                                fontFamily: "myfont"),
                                            size: 24.0,
                                            color: Colors.white,
                                          ),
                                        ),
                                      ),
                                    ),
                                    Expanded(
                                        child: Column(children: <Widget>[
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Text(
                                            "系统消息",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(16),
                                              color: Color(0xff000000),
                                            ),
                                          ),
                                          Text(
                                            provider.list[index].createTime ??
                                                "",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(14),
                                              color: Color(0xff999999),
                                            ),
                                          ),
                                        ],
                                      ),
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Container(
                                            width: width * 0.7,
                                            child: Text(
                                              provider.list[index].content ??
                                                  "",
                                              textAlign: TextAlign.left,
                                              style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(13),
                                                color: Color(0xff999999),
                                              ),
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                          ),
                                          provider.list[index].viewFlag == 0
                                              ? ClipRRect(
                                                  borderRadius:
                                                      BorderRadius.circular(3),
                                                  child: Container(
                                                      child: null,
                                                      width: 6,
                                                      height: 6,
                                                      color: Colors.red))
                                              : Container(child: null)
                                        ],
                                      ),
                                    ])),
                                  ]),
                            ),
                            onTap: () {
                              List<String> idList = [];
                              idList.add(provider.list[index].id);
                              changeNoticeState(idList);
                            },
                          );
                        },
                      );
                    }))
              ],
            ),
          ),
        ));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getWalletList(_page);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getWalletList(_page);
  }

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

class NoticeWidget extends StatelessWidget {
  NoticeWidget({Key key, this.list}) : super(key: key);
  List<dynamic> list;
  List<Widget> listWidget(context) => list.map((i) {
        return InkWell(
          child: Container(
            padding: EdgeInsets.only(
                left: ScreenUtil().setWidth(15),
                right: ScreenUtil().setWidth(15),
                top: ScreenUtil().setWidth(10),
                bottom: ScreenUtil().setWidth(10)),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 0.5, color: Colours.line),
              ),
            ),
            child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(right: ScreenUtil().setWidth(10)),
                    child: ClipRRect(
                      borderRadius:
                          BorderRadius.circular(ScreenUtil().setWidth(18)),
                      child: Container(
                        width: ScreenUtil().setWidth(36),
                        height: ScreenUtil().setWidth(36),
                        decoration: BoxDecoration(
                          gradient: const LinearGradient(
                              colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
                        ),
                        // padding: EdgeInsets.only(right:10),
                        child: Icon(
                          IconData(i.type == "system" ? 0xe609 : 0xe62f,
                              fontFamily: "myfont"),
                          size: 24.0,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                      child: Column(children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          i.type != null ? "系统消息" : "客服消息",
                          textAlign: TextAlign.left,
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(16),
                            color: Color(0xff000000),
                          ),
                        ),
                        Text(
                          i.createTime,
                          textAlign: TextAlign.left,
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(14),
                            color: Color(0xff999999),
                          ),
                        ),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          i.content,
                          textAlign: TextAlign.left,
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(13),
                            color: Color(0xff999999),
                          ),
                        ),
                        i.viewFlag != 0
                            ? ClipRRect(
                                borderRadius: BorderRadius.circular(3),
                                child: Container(
                                    child: null,
                                    width: 6,
                                    height: 6,
                                    color: Colors.red))
                            : Container(child: null)
                      ],
                    ),
                  ])),
                ]),
          ),
          onTap: () {
            print(i);
            NavigatorUtils.push(context, MeansRouter.errorDetail);
          },
        );
      }).toList();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(children: listWidget(context)),
    );
  }
}
