import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:image_picker_saver/image_picker_saver.dart';
import 'package:wanna_battle/model/PlayerInfo.dart';
import 'package:wanna_battle/net/HttpManager.dart';
import 'package:wanna_battle/net/Result.dart';
import 'package:wanna_battle/redux/AppState.dart';
import 'package:wanna_battle/styles/colors.dart';
import 'package:wanna_battle/styles/totast.dart';

class Appeal extends StatefulWidget {
  final PlayerInfo playerInfo;
  const Appeal(this.playerInfo, {Key key}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return AppealState();
  }
}

class AppealState extends State<Appeal> {
  String img1;
  String img2;
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        return Future.value(true);
      },
      child: Scaffold(
        backgroundColor: Color(0xFF3A3D5C),
        appBar: AppBar(
          title: Text('上传比赛结果'),
          centerTitle: true,
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: ListView(
                padding: EdgeInsets.only(left: 15, top: 18, right: 15, bottom: 18),
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.all(3),
                    child: Text(
                      '很遗憾，因为部分手机系统性能问题导致比赛结果上传失败，请你按照下图所示方式上传历史战绩以及本次游戏比赛详情战绩，工作人员审核后会根据实际情况进行名次的排序以及奖励的发放',
                      style: TextStyle(color: Colors.white, fontSize: 14),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 20),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.only(right: 7),
                            child: imgUpload('点击上传历史战绩截图', img1, (img) {
                              setState(() {
                                img1 = img;
                              });
                            }),
                          ),
                        ),
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.only(left: 7),
                            child: imgUpload('点击上传战绩详情截图', img2, (img) {
                              setState(() {
                                img2 = img;
                              });
                            }),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 30),
                    child: Text(
                      '历史战绩-图片示例',
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.white, fontSize: 14),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    child: Image.asset(
                      'images/shili_img_01.png',
                      fit: BoxFit.cover,
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 20),
                    child: Text(
                      '战绩详情-图片示例',
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.white, fontSize: 14),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    child: Image.asset(
                      'images/shili_img_02.png',
                      fit: BoxFit.cover,
                    ),
                  ),
                ],
              ),
            ),
            SafeArea(
              child: Container(
                height: 68,
                width: double.infinity,
                padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
                child: FlatButton(
                  color: PRIMARY_COLOR,
                  child: Text(
                    '提交',
                    style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold),
                  ),
                  onPressed: () async {
                    if (img1 == null || img1.isEmpty) {
                      Toast.show(context, '请上传历史战绩截图', 1500, 'info');
                    } else if (img2 == null || img2.isEmpty) {
                      Toast.show(context, '请上传战绩详情截图', 1500, 'info');
                    } else {
                      Toast.show(context, '加载中', -1, 'loading');
                      final res = await HttpManager.post('appealInfo/save', data: {
                        'userId': StoreProvider.of<AppState>(context).state.userInfo.id,
                        'playerInfoId': widget.playerInfo.id,
                        'houseId': widget.playerInfo.houseId,
                        'pic': img1 + ',' + img2,
                      });
                      Toast.hide();
                      if (res.success) {
                        Navigator.of(context).pop(true);
                      } else {
                        Toast.show(context, '提交失败，请稍后再试', 1500, 'info');
                      }
                    }
                  },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget imgUpload(String txt, String img, Function(String img) onUploadSuccess) {
    return GestureDetector(
      child: img != null && img.isNotEmpty
          ? Container(
              height: 111,
              decoration: BoxDecoration(border: Border.all(width: 1, color: Color(0xFF92434A)), color: Color(0xFF293354)),
              child: CachedNetworkImage(
                imageUrl: img,
                height: 111,
                fit: BoxFit.contain,
              ),
            )
          : Container(
              height: 111,
              decoration: BoxDecoration(border: Border.all(width: 1, color: Color(0xFF92434A)), color: Color(0xFF293354)),
              child: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset('images/icon_shangchuan.png', width: 34),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    child: Text(
                      txt,
                      style: TextStyle(color: PRIMARY_COLOR, fontSize: 13),
                    ),
                  )
                ],
              ),
            ),
      onTap: () async {
        File file = await ImagePickerSaver.pickImage(source: ImageSource.gallery, maxWidth: 1000);
        if (file == null) {
          return;
        }
        Toast.show(context, '加载中', -1, 'loading');
        Result res = await HttpManager.post('assets/uploadFile', data: {
          'file': UploadFileInfo(file, file.path),
        });
        Toast.hide();
        print(res.toJson());
        if (res.success) {
          onUploadSuccess(res.data[0]);
        }
      },
    );
  }
}
