import 'dart:io';

import 'package:chat/data/UserData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:chat/utils/TokenMgr.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

class UploadPicture extends StatefulWidget {
  @required
  final File img;

  UploadPicture({Key key, this.img}) : super(key: key);

  _UploadPictureState createState() => _UploadPictureState();
}

class _UploadPictureState extends State<UploadPicture> {
  bool isAgree = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          actions: <Widget>[
            Container(
              alignment: Alignment.center,
              child: new InkWell(
                child: new Padding(
                  padding:
                      EdgeInsets.only(right: 15, left: 15, top: 10, bottom: 10),
                  child: new Text(I18n.of(context).determine,
                      textScaleFactor: 1.0,
                      style: Constants.AppBarActionTextStyle),
                ),
                onTap: () async {
                  Map data = {"type": 2, "userId": UserData().basicInfo.userId};
                  data['sign'] = TokenMgr().getSign(data);
                  data['sex'] = UserData().basicInfo.sex;
                  data['isBurn'] = isAgree ? 1 : 0;
                  Response res = await HttpUtil().uploadFile(
                      widget.img, data, 'upload/file/postflie', 'image',
                      isShowLoading: true);
                  var resData = res.data;

                  if (resData['code'] == 0) {
                    if (resData['data']['msg'] != '') {
                      showToast(resData['data']['msg']);
                    }
                    resData['Type'] = isAgree ? 1 : 0;
                    MessageMgr().emit('refresh_photo');
                    Navigator.of(context).pop();
                  }
                },
              ),
            )
          ],
          centerTitle: true,
          leading: CustomUI.buildCustomLeading(context),
        ),
        body: SafeArea(
          child: Container(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            child: Column(
              children: <Widget>[
                Expanded(
                  child: Container(
                    color: Constants.BlackTextColor,
                    child: Image.file(
                      widget.img,
                    ),
                  ),
                ),
                Container(
                  height: 60,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Checkbox(
                        value: isAgree,
                        activeColor: Colors.blue,
                        onChanged: (bool val) {
                          this.setState(() {
                            isAgree = !isAgree;
                          });
                        },
                      ),
                      Text(I18n.of(context).destroy_after, textScaleFactor: 1.0)
                    ],
                  ),
                )
              ],
            ),
          ),
        ));
  }
}
