import 'package:flutter/material.dart';
import '../styles/colors.dart';
import 'dart:ui';
import '../styles/totast.dart';
import 'package:flutter_redux/flutter_redux.dart';
import '../redux/AppState.dart';
import '../net/HttpManager.dart';
import '../net/Result.dart';
import '../model/BindGameInfo.dart';
import '../widget/LinearButton.dart';


class BindGame extends StatefulWidget {
  @override
  BindGameState createState() => BindGameState();
}

class BindGameState extends State<BindGame> {
  String bindName = '';
  bool isBind = false;
  BindGameInfo bindInfo;

  Future<void> getBindInfo() async {
    Toast.show(context, '加载中', -1, 'loading');
    Result res = await HttpManager.get('bindGame/all', data: {
      'gameId': 1,
      'userId': StoreProvider.of<AppState>(context).state.userInfo.id
    });
    Toast.hide();
    if (res.success) {
      if (res.data.length > 0) {
        setState(() {
          isBind = true;
          bindInfo = BindGameInfo.fromJson(res.data[0]);
        });
      } else {
        setState(() {
          isBind = false;
        });
      }
    }
  }

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      getBindInfo();
    });
  }

  Future<void> bindEvent() async {
    if (bindName == '') {
      Toast.show(context, '请输入和平精英游戏编号', 1500, 'info');
      return;
    }
    Toast.show(context, '加载中', -1, 'loading');
    Result res = await HttpManager.post('bindGame/save', data: {
      'gameId': 1,
      'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
      'nickName': bindName
    });
    Toast.hide();
    if (res.success) {
      Toast.show(context, '绑定成功', 1000, 'success');
      getBindInfo();
    } else {}
  }

  Future<void> cancelBind() async {
    Toast.show(context, '加载中', -1, 'loading');
    final res =
        await HttpManager.post('bindGame/del', data: {'id': bindInfo.id});
    Toast.hide();
    if (res.success) {
      Toast.show(context, '解绑成功', 1000, 'success');
      getBindInfo();
    } else {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          // backgroundColor: PRIMARY_COLOR,
          title: Text('绑定账号'),
          centerTitle: true,
          elevation: 0,
        ),
        body: SingleChildScrollView(
          child: Container(
          color: BG_SUB_COLOR,
          padding: EdgeInsets.all(15),
          child: Column(
            children: <Widget>[
              Container(
                height: 220,
                width: double.infinity,
                // padding: EdgeInsets.symmetric(vertical: 15, horizontal: 34),
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage('images/img_chijizhanchang.png'),
                        fit: BoxFit.cover)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: _bindWidegt(),
                ),
              ),
              Container(
                height: 20,
              ),
              Text('游戏编号位置',style:TextStyle(color:Colors.white,fontSize:14)),
              Container(
                height: 10,
              ),
              Image.asset('images/bangding_img_01.png'),
               Container(
                height: 10,
              ),
              Image.asset('images/bangding_img_02.png')
            ],
          ),
        ),
        ));
  }

  List<Widget> _bindWidegt() {
    List<Widget> widgetList = [];
    if (isBind) {
      widgetList.add(Container(
        height: 78,
        padding: EdgeInsets.all(15),
        decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [Color(0xFF464B6A), Color(0xFF35395E)],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter)),
        child: Row(
          children: <Widget>[
            Image.network(bindInfo.gameInfo.icon, width: 48),
            Container(
              width: 10,
            ),
            Expanded(
                flex: 1,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(bindInfo.gameInfo.gameName,
                        style: TextStyle(color: Colors.white, fontSize: 14)),
                    Container(
                      height: 5,
                    ),
                    Row(
                      children: <Widget>[
                        Text(
                          '已绑定',
                          style: TextStyle(
                              color: Color(0xFF727785),
                              fontSize: 13,
                              fontWeight: FontWeight.w500),
                        ),
                        Container(
                          width: 10,
                        ),
                        Text(
                          bindInfo.nickName,
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 13,
                              fontWeight: FontWeight.w500),
                        )
                      ],
                    )
                  ],
                )),
            Container(
                width: 50,
                height: 30,
                child: OutlineButton(
                  padding: EdgeInsets.all(0),
                  borderSide: BorderSide(color:  Theme.of(context).primaryColor, width: 1),
                  textColor: Theme.of(context).primaryColor,
                  highlightColor: Color(0xFF16161C),
                  child: Text('解绑'),
                  onPressed: () => cancelBind(),
                ))
          ],
        ),
      ));
    } else {
      widgetList = [
        Container(
          margin: EdgeInsets.only(bottom: 10, left: 45, right: 45),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(2)),
            color: Colors.white,
          ),
          child: TextField(
            style: TextStyle(
              color: Color(0xFFFFC30F),
              fontWeight: FontWeight.w500,
            ),
            textAlign: TextAlign.center,
            decoration: InputDecoration(
              hintText: '请输入刺激战场游戏昵称',
              border: InputBorder.none,
              hintStyle: TextStyle(fontSize: 12, color: Color(0xFF727785)),
              contentPadding: const EdgeInsets.symmetric(vertical:10.0),
            ),
            onChanged: (value) {
              setState(() {
                bindName = value;
              });
            },
          ),
        ),
        Container(
            margin: EdgeInsets.only(left: 45, right: 45, bottom: 15),
            width: double.infinity,
            child: LinearButton(
              btntext: '绑定角色',
              btnHeight: 36.0,
              onTapHomeMenu: () => bindEvent(),
            ))
      ];
    }
    return widgetList;
  }
}
