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

import 'package:oktoast/oktoast.dart';
import '../data/UserData.dart';
import '../utils/TokenMgr.dart';

class MessagePushPage extends StatefulWidget {
  MessagePushPage({Key key}) : super(key: key);

  _MessagePushPageState createState() => _MessagePushPageState();
}

class _MessagePushPageState extends State<MessagePushPage> {
  bool isMan = true;

  //私聊信息
  bool privatyMsg = false;
  //消息推送
  bool msgPush = false;
  //男士申请查看资料页
  bool watchMyData = false;
  //男士查看社交账号
  bool watchSocialAccount = false;
  //查看红包照片
  bool watchMyPicture = false;
  //男士给我发送社交账号
  bool sendSocialAccount = false;
  //新约会
  bool newDate = false;
  //女士通过我的查看请求
  bool womenAgree = false;
  //邀请码申请成功
  bool codeSucess = false;

  bool isLoadingFish = false;

  BoxDecoration _getCardDecoration() {
    return new BoxDecoration(
      color: Colors.white,
    );
  }

  getSettingInfo() async {
    Map data = {
      "userId": UserData().basicInfo.userId,
      "sex": UserData().basicInfo.sex,
    };
    data['sign'] = TokenMgr().getSign(data);
    try {
      Response res = await HttpUtil().post('message/push/message',
          data: data, failback: () => Navigator.of(context).pop());
      Map resData = res.data;
      if (resData['code'] == 0) {
        privatyMsg = resData['data']['PrivacyChat'] == 1;
        newDate = resData['data']['NewBroadcast'] == 1;
        if (isMan) {
          womenAgree = resData['data']['AcceptCheck'] == 1;
          codeSucess = resData['data']['ApplySuccess'] == 1;
        } else {
          watchMyData = resData['data']['ApplyCheck'] == 1;
          watchMyPicture = resData['data']['CheckPhoto'] == 1;
        }
        Future.delayed(Duration(milliseconds: Constants.CloseLoaindTime), () {
          isLoadingFish = true;
          setState(() {});
        });
        setState(() {});
      }
    } catch (e) {}
  }

  @override
  void initState() {
    super.initState();
    isMan = UserData().isMan();
    getSettingInfo();
  }

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      backgroundColor: AppColors.NewAppbarBgColor,
      title: new Text(
        I18n.of(context).push_setting2,
        textScaleFactor: 1.0,
        style: TextStyle(color: AppColors.NewAppbarTextColor),
      ),
      leading: CustomUI.buildCustomLeading(context),
      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).save,
                  textScaleFactor: 1.0, style: Constants.AppBarActionTextStyle),
            ),
            onTap: () async {
              Map data = {
                "userId": UserData().basicInfo.userId,
                "sex": UserData().basicInfo.sex,
              };
              data['sign'] = TokenMgr().getSign(data);
              data['privacyChat'] = privatyMsg ? 1 : 0;
              data['newBroadcast'] = newDate ? 1 : 0;
              if (isMan) {
                data['acceptCheck'] = womenAgree ? 1 : 0;
                data['applySuccess'] = codeSucess ? 1 : 0;
              } else {
                data['applyCheck'] = watchMyData ? 1 : 0;
                data['checkPhoto'] = watchMyPicture ? 1 : 0;
                data['checkAccount'] = 1;
                data['sendAccount'] = 1;
              }
              try {
                Response res = await HttpUtil()
                    .post('message/setting/message', data: data);
                Map resData = res.data;
                showToast('${resData['msg']}');

                if (resData['code'] == 0) {
                  UserData().privatyMsgPushSwitch = privatyMsg;
                  UserData().newDateSwitch = newDate;
                  UserData().applyCheckSwitch = watchMyData;
                  UserData().acceptCheckSwitch = womenAgree;
                  UserData().checkPhotoSwitch = watchMyPicture;
                  UserData().codeSucessSwitch = codeSucess;
                }
              } catch (e) {}
            },
          ),
        )
      ],
      centerTitle: true,
    );
    Widget content = Scaffold(
      body: SafeArea(
          child: Center(
        child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: _buildBody(),
        ),
      )),
      appBar: appBar,
    );
    return CustomUI.buildPageLoading(context, content, !isLoadingFish);
  }

  Widget _buildBody() {
    List<Widget> man = [
      _buildHiddenButtom(I18n.of(context).private_chat2, privatyMsg, () {
        setState(() {
          privatyMsg = !privatyMsg;
        });
      }),
      _buildHiddenButtom(I18n.of(context).new_date, newDate, () {
        setState(() {
          newDate = !newDate;
        });
      }),
      _buildHiddenButtom(I18n.of(context).passed_request2, womenAgree, () {
        setState(() {
          womenAgree = !womenAgree;
        });
      }),
      // _buildHiddenButtom(I18n.of(context).applied_successfully, codeSucess, () {
      //   setState(() {
      //     codeSucess = !codeSucess;
      //   });
      // }),
    ];
    List<Widget> women = [
      _buildHiddenButtom(I18n.of(context).private_chat2, privatyMsg, () {
        setState(() {
          privatyMsg = !privatyMsg;
        });
      }),
      _buildHiddenButtom(I18n.of(context).new_date, newDate, () {
        setState(() {
          newDate = !newDate;
        });
      }),
      _buildHiddenButtom(I18n.of(context).men_view, watchMyData, () {
        setState(() {
          watchMyData = !watchMyData;
        });
      }),
      _buildHiddenButtom(I18n.of(context).men_view3, watchMyPicture, () {
        setState(() {
          watchMyPicture = !watchMyPicture;
        });
      }),
    ];
    return new ListView(
      children: isMan ? man : women,
    );
  }

  //是否隐藏社交账号
  Widget _buildHiddenButtom(str, value, callback) {
    Widget left = new Text(
      str,
      textScaleFactor: 1.0,
      style: TextStyle(fontSize: 14, fontWeight: FontWeight.normal),
    );
    Widget right = new Expanded(
        child: new Align(
            alignment: Alignment.centerRight,
            child: new Switch(
              value: value,
              activeTrackColor: Constants.ConfrimButtonColor.withOpacity(0.3),
              onChanged: (bool val) {
                callback();
              },
            )));
    return new Container(
      decoration: _getCardDecoration(),
      margin: EdgeInsets.only(top: 15, bottom: 0),
      padding: EdgeInsets.only(left: 15),
      height: 53,
      child: new Row(
        children: <Widget>[left, right],
      ),
    );
  }
}
