import 'package:chat/data/UserData.dart';
import 'package:chat/data/WebData.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/TokenMgr.dart';
import 'package:chat/utils/screen.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:oktoast/oktoast.dart';

import 'InformUser.dart';

class ApplyContent extends StatefulWidget {
  final userInfo;
  final userId;
  final bool isMan;

  ApplyContent({Key key, this.userInfo, this.userId, this.isMan = false})
      : super(key: key);

  _ApplyContentState createState() => _ApplyContentState();
}

class _ApplyContentState extends State<ApplyContent> {
  int friendly = 0;
  int interesting = 0;
  int refreshing = 0;
  int patience = 0;
  int hello = 0;
  int unfriendly = 0;
  bool isPost = false;

  Set select = new Set();

  String evilImg1 = ''; //恶意差评1
  String evilImg2 = ''; //恶意差评2

  @override
  Widget build(BuildContext context) {
    String tip = isPost
        ? I18n.of(context).anonymous_evaluation
        : (widget.isMan
            ? I18n.of(context).his_evaluation
            : I18n.of(context).her_evaluation);
    return CustomUI.buildTipContent(
        context, tip, _buildContent(widget.userInfo));
  }

  @override
  void initState() {
    super.initState();
    friendly = widget.userInfo['Friendly'];
    interesting = widget.userInfo['Interesting'];
    refreshing = widget.userInfo['Refreshing'];
    patience = widget.userInfo['Patience'];
    hello = widget.userInfo['Hello'];
    unfriendly = widget.userInfo['Unfriendly'];
  }

  void doContext(callback) async {
    Map data = {
      "userid": widget.userId,
      "evaluateuserid": UserData().basicInfo.userId,
    };
    String str = '';
    select.forEach((s) {
      str += (str == '' ? s : '|$s');
    });
    if (str == '') {
      showToast(I18n.of(context).click_select);
      return;
    }
    data['sign'] = TokenMgr().getSign(data);
    data['value'] = str;
    data['evilImg'] = '$evilImg1|$evilImg2';
    Response res = await HttpUtil().post('evaluate/user/insert', data: data);
    var resData = res.data;
    if (resData['code'] == 0) {
      select.forEach((s) {
        switch (s) {
          case 'Friendly':
            friendly++;
            break;
          case 'Interesting':
            interesting++;
            break;
          case 'Refreshing':
            refreshing++;
            break;
          case 'Patience':
            patience++;
            break;
          case 'Hello':
            hello++;
            break;
          case 'Unfriendly':
            unfriendly++;
            break;
          default:
        }
      });
      showToast(resData['msg']);
      callback();
    } else {
      showToast(resData['msg']);
    }
  }

  void _sendPicture(callback) async {
    if (await CustomUI.showPhotoPermissionSetting(context)) {
      var tempFile = await ImagePicker.pickImage(source: ImageSource.gallery);
      if (tempFile != null) {
        Map data = {"type": 4, "userId": UserData().basicInfo.userId};
        data['sign'] = TokenMgr().getSign(data);

        Response res = await HttpUtil().uploadFile(
            tempFile, data, 'upload/file/postflie', 'image',
            isShowLoading: true);

        var resData = res.data;
        if (resData['code'] == 0) {
          Navigator.of(context).pop();
          callback(WebData().deleteDemain(resData['msg']));
        }
      }
    }
  }

  Widget _buildBox(str, value, callback) {
    bool isSelect = select.contains(value);
    return InkWell(
        onTap: callback,
        child: Container(
          alignment: Alignment.center,
          margin: EdgeInsets.only(top: 10, bottom: 21, left: 5, right: 5),
          width: (Screen.width - 70) / 3,
          height: 28,
          decoration: BoxDecoration(
              color: isSelect ? Colors.blue : Colors.grey[200],
              borderRadius:
                  BorderRadius.circular(Constants.LittleButtonRadius)),
          child: Text(
            str,
            textScaleFactor: 1.0,
            style: TextStyle(
                fontSize: 14,
                color: isSelect ? Colors.white : Constants.BlackTextColor),
          ),
        ));
  }

  Widget _buildContext(nums, str) {
    return InkWell(
        onTap: () {
          if (widget.userInfo['Statu'] == 1) {
            showToast(I18n.of(context).has_eva);
            return;
          }
          if (widget.userInfo['IsComment'] == 0) {
            showToast(!widget.isMan
                ? I18n.of(context).cant_evaluate
                : I18n.of(context).cant_evaluate2);
            return;
          }
          setState(() {
            isPost = !isPost;
          });
        },
        child: CustomUI.buildBox(nums, str));
  }

  Widget _buildContent(data) {
    var evaluation1 =
        widget.isMan ? I18n.of(context).courtesy : I18n.of(context).friendly;

    var evaluation2 = I18n.of(context).interesting;
    var evaluation3 = I18n.of(context).readily;
    var evaluation4 =
        widget.isMan ? I18n.of(context).generous : I18n.of(context).patient;
    var evaluation5 =
        widget.isMan ? I18n.of(context).lie : I18n.of(context).indifferent;
    var evaluation6 = widget.isMan
        ? I18n.of(context).unfriendly
        : I18n.of(context).bad_temper;

    return Container(
        padding: EdgeInsets.only(top: 10),
        child: Column(
          children: <Widget>[
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                isPost
                    ? _buildBox(evaluation1, 'Friendly', () {
                        select.contains('Friendly')
                            ? select.remove('Friendly')
                            : select.add('Friendly');
                        setState(() {});
                      })
                    : _buildContext(friendly, evaluation1),
                isPost
                    ? _buildBox(evaluation2, 'Interesting', () {
                        select.contains('Interesting')
                            ? select.remove('Interesting')
                            : select.add('Interesting');
                        setState(() {});
                      })
                    : _buildContext(interesting, evaluation2),
                isPost
                    ? _buildBox(evaluation3, 'Refreshing', () {
                        select.contains('Refreshing')
                            ? select.remove('Refreshing')
                            : select.add('Refreshing');
                        setState(() {});
                      })
                    : _buildContext(refreshing, evaluation3),
              ],
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                isPost
                    ? _buildBox(evaluation4, 'Patience', () {
                        select.contains('Patience')
                            ? select.remove('Patience')
                            : select.add('Patience');
                        setState(() {});
                      })
                    : _buildContext(patience, evaluation4),
                isPost
                    ? _buildBox(evaluation5, 'Hello', () {
                        if (select.contains('Hello')) {
                          select.remove('Hello');
                          evilImg1 = '';
                          setState(() {});
                        } else {
                          CustomUI.buildOneConfirm(
                              context,
                              I18n.of(context).bad_evaluate,
                              I18n.of(context).determine, () {
                            _sendPicture((url) {
                              evilImg1 = url;
                              select.add('Hello');
                              setState(() {});
                            });
                          });
                        }
                      })
                    : _buildContext(hello, evaluation5),
                isPost
                    ? _buildBox(evaluation6, 'Unfriendly', () {
                        if (select.contains('Unfriendly')) {
                          select.remove('Unfriendly');
                          evilImg2 = '';
                          setState(() {});
                        } else {
                          CustomUI.buildOneConfirm(
                              context,
                              I18n.of(context).bad_evaluate,
                              I18n.of(context).determine, () {
                            _sendPicture((url) {
                              evilImg2 = url;
                              select.add('Unfriendly');
                              setState(() {});
                            });
                          });
                        }
                      })
                    : _buildContext(unfriendly, evaluation6),
              ],
            ),
            InkWell(
                onTap: () {
                  if (!isPost && widget.userInfo['Statu'] == 1) {
                    showToast(I18n.of(context).has_eva);
                    return;
                  }
                  if (widget.userInfo['IsComment'] == 0) {
                    showToast(!widget.isMan
                        ? I18n.of(context).cant_evaluate
                        : I18n.of(context).cant_evaluate2);
                    return;
                  }
                  if (isPost) {
                    doContext(() {
                      isPost = !isPost;
                      widget.userInfo['Statu'] = 1;
                      setState(() {});
                    });
                  } else {
                    isPost = !isPost;
                    setState(() {});
                  }
                },
                child: Container(
                  margin: EdgeInsets.only(top: 20, left: 80, right: 80),
                  width: double.infinity,
                  alignment: Alignment.center,
                  padding: EdgeInsets.only(top: 8, bottom: 8),
                  decoration: BoxDecoration(
                      border:
                          Border.all(color: const Color(0xFF2D81FF), width: 1),
                      color: const Color(0xFF2D81FF),
                      borderRadius: BorderRadius.all(
                          Radius.circular(Constants.LittleButtonRadius))),
                  child: Text(
                    isPost
                        ? I18n.of(context).submit
                        : I18n.of(context).anonymous_evaluation,
                    textScaleFactor: 1.0,
                    style: TextStyle(color: Colors.white),
                  ),
                )),
            InkWell(
                onTap: () {
                  Navigator.of(context).push(
                    new MaterialPageRoute(
                      builder: (context) {
                        return InformUserPage(
                          isMan: widget.isMan,
                          userId: widget.userId,
                        );
                      },
                    ),
                  );
                },
                child: Container(
                  margin: EdgeInsets.only(top: 5),
                  child: Text(
                    I18n.of(context).anonymous_report,
                    textScaleFactor: 1.0,
                    style: TextStyle(color: Colors.red, fontSize: 12),
                  ),
                ))
          ],
        ));
  }
}
