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/models/ref_name_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class InfoType {
  static const int Apply = 1;
  static const int Evaluation = 2;
  static const int System = 3;
  static const int Radio = 4;
  static const int Money = 5;
  static const int IncomeMoney = 6;
  static const int MyMoney = 7;
}

class TitleItem {
  String title = '';
  String name = '';
  String value = '';
  TitleItem({this.title, this.name, this.value});
}

class RichTitle {
  static List<InlineSpan> getRichText(TitleItem titleItem,
      {titleStyle, nameStyle}) {
    if (nameStyle == null) {
      nameStyle = TextStyle(
          fontWeight: FontWeight.w500, color: Constants.BlackTextColor);
    }
    if (titleStyle == null) {
      titleStyle = TextStyle(color: Constants.GreyTextColor);
    }

    titleItem.title = titleItem.title == null ? '' : titleItem.title;
    List list = titleItem.title.split('/s1');
    titleItem.name = titleItem.name == null ? '' : titleItem.name;
    List<InlineSpan> child;
    if (list == null) {
      child = [
        TextSpan(text: titleItem.title, style: titleStyle),
      ];
    } else {
      child = list.length > 1
          ? [
              TextSpan(text: list[0] == '' ? '' : list[0], style: titleStyle),
              TextSpan(text: titleItem.name, style: nameStyle),
              TextSpan(text: list[1], style: titleStyle),
            ]
          : [
              TextSpan(text: titleItem.name, style: nameStyle),
              TextSpan(text: list[0], style: titleStyle),
            ];
    }
    return child;
  }

  static String getNormalText(TitleItem titleItem) {
    List list = titleItem.title.split('/s1');
    titleItem.name = titleItem.name == null ? '' : titleItem.name;
    String res = '';
    if (list == null) {
      res = titleItem.title;
    } else {
      list.length > 1
          ? res = (list[0] == '' ? list[0] : list[0]) + titleItem.name + list[1]
          : res = titleItem.name + list[0];
    }
    return res;
  }

  //申请
  static TitleItem _delApplyTitle(userInfo, BuildContext context) {
    print(userInfo);
    bool isMyself = userInfo['ApplyUserId'] == UserData().basicInfo.userId;

    String name = isMyself
        ? Provider.of<RefNameProvider>(context)
            .getRefName(userInfo['UserId'], userInfo['UserName'])
        : Provider.of<RefNameProvider>(context)
            .getRefName(userInfo['ApplyUserId'], userInfo['ApplyName']);
    String title = isMyself
        ? userInfo['Status'] == 1
            ? I18n.of(context).l15_days
            : I18n.of(context).reject_reply
        : I18n.of(context).view_application2;
    if (userInfo['Type'] == 1) {
      title = isMyself
          ? I18n.of(context).applyed_friends
          : I18n.of(context).apply_friends;
    }

    return TitleItem(title: title, name: name);
  }

  //评价
  static TitleItem _delEvaluateTitle(userInfo, BuildContext context) {
    String name = WebData().getEvaluation(context, userInfo['Value']);
    String title = '';
    if (userInfo['Status'] == 2) {
      title = I18n.of(context).successful_appeal;
    } else if (userInfo['Status'] == 3) {
      title = I18n.of(context).successful_appeal1;
    } else {
      title = UserData().isMan()
          ? I18n.of(context).received_evaluation
          : I18n.of(context).received_evaluation2;
    }
    return TitleItem(title: title, name: name);
  }

  //电台
  static TitleItem _delRadioTitle(data, BuildContext context) {
    bool isProgram = data['ProgramType'] == 0;
    String title;
    switch (data['Type']) {
      case 1:
        title = isProgram
            ? I18n.of(context).thumbs_up2
            : I18n.of(context).thumbs_up;
        break;
      case 2:
        title =
            isProgram ? I18n.of(context).comment2 : I18n.of(context).comment1;
        break;
      case 3:
        title = I18n.of(context).signed_up;
        break;
      case 4:
        title = isProgram
            ? I18n.of(context).favorite_user
            : I18n.of(context).favorite_user2;
        break;
      case 5:
        title = I18n.of(context).replied_comment;
        break;
      default:
    }
    return TitleItem(
        title: title,
        name: Provider.of<RefNameProvider>(context)
            .getRefName(data['UserId'], data['UserName']));
  }

  //系统
  static TitleItem _delSystemTitle(data, BuildContext context) {
    bool isMyself = data['UserId'] == UserData().basicInfo.userId;

    bool isPass = data['Status'] == 1;
    String title = '';
    String name = Provider.of<RefNameProvider>(context)
        .getRefName(data['ReportedUserId'], data['ReportedUserName']);
    switch (data['Type']) {
      case 1:
        title = isPass
            ? I18n.of(context).upload_success
            : I18n.of(context).re_upload;
        break;
      case 2:
        title = isPass
            ? I18n.of(context).successful_authentication
            : I18n.of(context).authentication_failed;
        break;
      case 3:
        if (isMyself) {
          title = data['Status'] == 1
              ? I18n.of(context).report_success
              : I18n.of(context).report_failure;
        } else {
          title = I18n.of(context).coin_returen;
        }
        break;
      case 4:
        title = data['Status'] == 1
            ? I18n.of(context).successful_application
            : I18n.of(context).application_failed;
        name = data['Status'] == 1 ? data['Content'].toString() : null;
        break;
      case 5:
        title = '代理会员推送相关';
        break;
      default:
    }
    return TitleItem(title: title, name: name);
  }

  //钱包
  static TitleItem _delMoneyTitle(data, BuildContext context) {
    String title = '';
    bool isMyself = data['UserId'] == UserData().basicInfo.userId;
    String name = Provider.of<RefNameProvider>(context)
        .getRefName(data['ChangeUserId'], data['UserName']);
    switch (data['DetailType'] == 10 ? data['Status'] : data['DetailType']) {
      case 1:
        title = I18n.of(context).get_coin2;
        name = data['Value'].toString();
        break;
      case 2:
        title = I18n.of(context).buy_vip;
        name = data['Value'].toString();
        break;
      case 3:
        title = isMyself
            ? I18n.of(context)
                .paid_you4
                .replaceFirst('/s2', data['Value'].toString())
            : I18n.of(context)
                .paid_you
                .replaceFirst('/s2', data['Value'].toString());
        break;
      case 4:
        title = isMyself
            ? I18n.of(context)
                .paid_you5
                .replaceFirst('/s2', data['Value'].toString())
            : I18n.of(context)
                .paid_you2
                .replaceFirst('/s2', data['Value'].toString());
        break;
      case 5:
        title = I18n.of(context).buy_program;
        name = data['Value'].toString();
        break;
      case 6:
        if (data['Status'] == 0) {
          title = I18n.of(context).del_add_money;
        } else if (data['Status'] == 1) {
          title = I18n.of(context).adding_money;
        } else {
          title = I18n.of(context).withdraw_fail;
        }
        name = data['Value'].toString();
        break;
      case 7:
        title =
            isMyself ? I18n.of(context).paid_you6 : I18n.of(context).paid_you3;
        break;
      case 8:
        title = isMyself
            ? I18n.of(context)
                .give_other_money
                .replaceFirst('/s2', data['Value'].toString())
            : I18n.of(context)
                .get_other_money
                .replaceFirst('/s2', data['Value'].toString());
        break;
      case 9:
        title = isMyself
            ? I18n.of(context)
                .give_red_money
                .replaceFirst('/s2', data['Value'].toString())
            : I18n.of(context)
                .get_red_money
                .replaceFirst('/s2', data['Value'].toString());
        break;
      case 11:
        title = I18n.of(context).Redeem;
        name = data['Value'].toString();
        break;
    }
    return TitleItem(title: title, name: name);
  }

  //收入金额
  static TitleItem _delInMoneyTitle(data, BuildContext context) {
    String title = '';
    String name = Provider.of<RefNameProvider>(context)
        .getRefName(data['UserId'], data['UserName']);
    switch (data['DetailType']) {
      case 1:
        if (data['Status'] == 0) {
          title = I18n.of(context).del_add_money;
        } else if (data['Status'] == 1) {
          title = I18n.of(context).withdraw_fail;
        } else {
          title = I18n.of(context).adding_money;
        }

        name = data['Value'].toString();
        break;
      case 2:
        title = I18n.of(context).change_h_coin;
        name = data['Value'].toString();
        break;
      case 3:
        title = I18n.of(context)
            .get_other_money
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 4:
        title = I18n.of(context)
            .get_red_money
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 5:
        title = I18n.of(context).paid_you3;
        break;
      case 6:
        title = I18n.of(context)
            .paid_you
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 7:
        title = I18n.of(context)
            .paid_you2
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 8:
        title = I18n.of(context).Representation_succes;
        break;
      case 9:
        title = I18n.of(context).system_give;
        break;
      case 10:
        title = I18n.of(context).exchange_fail;
        break;
    }
    return TitleItem(title: title, name: name);
  }

  //我的余额
  static TitleItem _delMyMoneyTitle(data, BuildContext context) {
    String title = '';
    String name = Provider.of<RefNameProvider>(context)
        .getRefName(data['UserId'], data['UserName']);
    switch (data['DetailType']) {
      case 1:
        title = I18n.of(context).get_coin2;
        name = data['Value'].toString();
        break;
      case 2:
        title = I18n.of(context).hibok_exchange;
        name = data['Value'].toString();
        break;
      case 3:
        title = I18n.of(context).bind_code_success;
        break;
      case 4:
        title = I18n.of(context)
            .give_other_money
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 5:
        title = I18n.of(context)
            .give_red_money
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 6:
        title = I18n.of(context)
            .paid_you6
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 7:
        title = I18n.of(context)
            .paid_you4
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 8:
        title = I18n.of(context)
            .paid_you5
            .replaceFirst('/s2', data['Value'].toString());
        break;
      case 9:
        title = I18n.of(context).buy_vip;
        name = data['Value'].toString();
        break;
      case 10:
        title = I18n.of(context).translate_money;
        name = data['Value'].toString();
        break;
      case 11:
        title = I18n.of(context).buy_program;
        name = data['Value'].toString();
        break;
      case 12:
        title = I18n.of(context).Representation_succes;
        break;
      case 13:
        title = I18n.of(context).system_back;
        break;
      case 14:
        title = I18n.of(context).system_give;
        break;
    }
    return TitleItem(title: title, name: name);
  }

  static TitleItem _getTypeTitleItem(userInfo, BuildContext context, int type) {
    var tileItem = TitleItem();
    switch (type) {
      case InfoType.Apply:
        tileItem = _delApplyTitle(userInfo, context);
        break;
      case InfoType.Radio:
        tileItem = _delRadioTitle(userInfo, context);
        break;
      case InfoType.System:
        tileItem = _delSystemTitle(userInfo, context);
        break;
      case InfoType.Money:
        tileItem = _delMoneyTitle(userInfo, context);
        break;
      case InfoType.IncomeMoney:
        tileItem = _delInMoneyTitle(userInfo, context);
        break;
      case InfoType.MyMoney:
        tileItem = _delMyMoneyTitle(userInfo, context);
        break;
      case InfoType.Evaluation:
        tileItem = _delEvaluateTitle(userInfo, context);
        break;
      default:
    }
    return tileItem;
  }

  static Widget getRichTitleWidget(userInfo, BuildContext context, int type,
      {titleStyle, nameStyle}) {
    var tileItem = _getTypeTitleItem(userInfo, context, type);
    return RichText(
        text: TextSpan(
            children: getRichText(tileItem,
                titleStyle: titleStyle, nameStyle: nameStyle)));
  }

  static String normalTitle(userInfo, BuildContext context, int type) {
    if (userInfo == null) {
      return '';
    }
    var tileItem = _getTypeTitleItem(userInfo, context, type);
    var res = getNormalText(tileItem);
    int maxlength = 17;
    return res.length > maxlength ? res.substring(0, maxlength) + '...' : res;
  }
}
