import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_redux/flutter_redux.dart';
import '../redux/AppState.dart';
import '../net/Result.dart';
import '../net/HttpManager.dart';
import '../redux/UserRedux.dart';
import '../model/UserInfo.dart';

//竞赛须知
class CompetitionInformation extends StatefulWidget {
  @override
  CompetitionInformationState createState() => CompetitionInformationState();
}

class CompetitionInformationState extends State<CompetitionInformation> {
  int nowSwiperIndex = 0;
  SwiperController _swiperController;
  @override
  void initState() {
    super.initState();
    _swiperController = SwiperController();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        backgroundColor: Colors.black87,
        body: Container(
            child: Center(
                child: Swiper(
                    controller: _swiperController,
                    layout: SwiperLayout.DEFAULT,
                    itemWidth: 270.0,
                    itemHeight: 422.0,
                    control: SwiperControl(
                        color: Color(0xFFC2524D),
                        disableColor: Colors.transparent),
                    index: nowSwiperIndex,
                    itemCount: 4,
                    scrollDirection: Axis.horizontal,
                    loop: false,
                    itemBuilder: (context, index) {
                      return InfoBox(
                          typeIndex: index + 1,
                          next: () {
                            _swiperController.next();
                          },
                          end: () {
                            Navigator.of(context).pop();
                          });
                    }))),
      ),
      onWillPop: () {
        return Future.value(false);
      },
    );
  }
}

typedef int OnTapHomeMenu();

class InfoBox extends StatelessWidget {
  InfoBox({Key key, this.typeIndex, this.next, this.end}) : super(key: key);
  final int typeIndex;
  final OnTapHomeMenu next;
  final OnTapHomeMenu end;
  @override
  Widget build(BuildContext context) {
    return UnconstrainedBox(
        child: Container(
      width: 270,
      height: 422,
      decoration: BoxDecoration(
          border: Border.all(width: 1, color: Color(0xFFC2524D)),
          color: Colors.black),
      child: Stack(
        children: <Widget>[
          Container(
            width: 270,
            padding: EdgeInsets.only(top: 25),
            child: Column(
              children: <Widget>[
                Text('竞赛须知 ' + typeIndex.toString() + '/4',
                    style: TextStyle(
                        color: Color(0xFFFDC372),
                        fontSize: 20,
                        fontWeight: FontWeight.w600)),
                _centerContent(typeIndex)
              ],
            ),
          ),
          Positioned(
            top: 0,
            left: 0,
            child: Image.asset(
              'images/tancuang_shang.png',
              width: 131,
            ),
          ),
          Positioned(
            bottom: 0,
            right: 4,
            child: Image.asset(
              'images/tancuang_xia.png',
              width: 148,
            ),
          ),
          Positioned(
            bottom: 15,
            left: 25,
            child: Column(
              children: <Widget>[
                Container(
                  width: 220,
                  height: 36,
                  child: RaisedButton(
                    textColor: Colors.white,
                    child: Text(typeIndex != 4 ? '下一个' : '结束'),
                    onPressed: typeIndex != 4 ? next : end,
                  ),
                ),
                FlatButton(
                  textColor: Color(0xFF727785),
                  highlightColor: Colors.transparent,
                  splashColor: Colors.transparent,
                  child: Text('不再提醒'),
                  onPressed: () async {
                    Result res =
                        await HttpManager.post('userInfo/update', data: {
                      "id":
                          StoreProvider.of<AppState>(context).state.userInfo.id,
                      'remindFlag': 'N'
                    });
                    if (res.success) {
                      Result res2 =
                          await HttpManager.get("userInfo/getUserInfo");
                      if (res2.success) {
                        StoreProvider.of<AppState>(context).dispatch(
                            UpdateUserAction(UserInfo.fromJson(res2.data)));
                      }
                    }

                    Navigator.of(context).pop();
                  },
                )
              ],
            ),
          )
        ],
      ),
    ));
  }

  Widget _centerContent(int index) {
    Widget useContent = Container();
    TextStyle _text = TextStyle(color: Colors.white, fontSize: 14);
    if (index == 1) {
      useContent = Container(
        margin: EdgeInsets.only(top: 42),
        child: Column(
          children: <Widget>[
            Image.asset('images/icon_jinbi_da_hong.png', width: 30),
            Container(
              height: 20,
            ),
            Text(
              '房间人数越多，总奖金就越多',
              style: _text,
            ),
            Text('第一名获得50%', style: _text),
            Text('第二名获得30%', style: _text),
            Text('第三名获得20%', style: _text),
            Text('其他名次没有奖励', style: _text)
          ],
        ),
      );
    } else if (index == 2) {
      useContent = Container(
        margin: EdgeInsets.only(top: 20),
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: <Widget>[
            Image.asset('images/img_xuzhi_02.png', width: 136),
            Container(
              height: 10,
            ),
            Text.rich(
                TextSpan(children: [
                  TextSpan(
                    text: '竞赛开始前请在此耐心等待，在开始时会有上图所示弹窗，玩家必须在10秒内',
                    style: _text,
                  ),
                  TextSpan(
                    text: '点击确认',
                    style: TextStyle(
                        color: Color(0xFFC2524D),
                        fontSize: 14,
                        fontWeight: FontWeight.w500),
                  ),
                  TextSpan(
                    text: '，否则视为放弃此次竞赛，已支付金币概不退换',
                    style: _text,
                  )
                ]),
                textAlign: TextAlign.center)
          ],
        ),
      );
    } else if (index == 3) {
      useContent = Container(
        margin: EdgeInsets.only(top: 20),
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: <Widget>[
            Image.asset('images/img_xuzhi_03.png', width: 176),
            Container(
              height: 10,
            ),
            Text.rich(
                TextSpan(children: [
                  TextSpan(
                    text: '确认竞赛后会有弹窗提示授权进行录屏的操作，请一定',
                    style: _text,
                  ),
                  TextSpan(
                    text: '点击“确定”',
                    style: TextStyle(
                        color: Color(0xFFC2524D),
                        fontSize: 14,
                        fontWeight: FontWeight.w500),
                  ),
                  TextSpan(
                    text: '或“允许”此操作，否则竞赛判定失败',
                    style: _text,
                  )
                ]),
                textAlign: TextAlign.center)
          ],
        ),
      );
    } else if (index == 4) {
      useContent = Container(
        margin: EdgeInsets.only(top: 20),
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: <Widget>[
            Image.asset('images/img_xuzhi_04.png', width: 216),
            Container(
              height: 10,
            ),
            Text.rich(
                TextSpan(children: [
                  TextSpan(
                    text: '在游戏中吃鸡后，',
                    style: _text,
                  ),
                  TextSpan(
                    text: '请一定一定一定要点击“继续”按钮',
                    style: TextStyle(
                        color: Color(0xFFC2524D),
                        fontSize: 14,
                        fontWeight: FontWeight.w600),
                  ),
                  TextSpan(
                    text: '，显示到上图所示画面，然后再切换到我们全民APP中，点击完成比赛，方可成功上传本次成绩，赢取大奖',
                    style: _text,
                  )
                ]),
                textAlign: TextAlign.center)
          ],
        ),
      );
    }
    return useContent;
  }
}
