/*
 * @author lsy
 * @date   2019-09-04
 **/

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gmalpha_flutter/Annotations/RouterCenterRestore.mark.dart';
import 'package:gmalpha_flutter/commonModel/GMBase.dart';
import 'package:gmalpha_flutter/commonModel/base/BaseUtil.dart';
import 'package:gmalpha_flutter/commonModel/live/BaseModel.dart';
import 'package:gmalpha_flutter/commonModel/net/Api.dart';
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/commonModel/toast/toast.dart';
import 'package:gmalpha_flutter/userModel/service/UserRepository.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.user.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserResultBean.dart';
import 'package:gmalpha_flutter/userModel/util/JumpUtil.dart';

import '../../../commonModel/live/LiveData.dart';

class UserSettingModel extends BaseModel {
  LiveData<String> nameLive = new LiveData();
  LiveData<String> cityLive = new LiveData();
  LiveData<String> headImgLive = new LiveData();
  LiveData<bool> saveLive = new LiveData();

  final String userid;
  final String refere;
  String selectImgPath, defultName, defultCity;

  UserSettingModel(this.userid, this.refere);

  init(BuildContext context) {
    UserEntityImpl().getuserId().listen((value) {
      print("VALUEE  $value");
      if (value == null || value != userid) {
        UserRepository.getInstance().getUserInfo(userid).listen((data) {
          print(data);
          nameLive.notifyView(data?.data?.nickName);
          cityLive.notifyView(data?.data?.countryInfo?.name);
          headImgLive.notifyView(data?.data?.profilePic);
          defultName = data?.data?.nickName;
          defultCity = data?.data?.countryInfo?.name;
        }).onError((error) {
          print(error);
          Toast.show(context, error);
        });
      } else {
        UserEntityImpl().getnickName().listen((data) {
          print("NICK $data");
          defultName = data;
          nameLive.notifyView(data);
        }).onError((error) {
          print(error);
          Toast.debugShow(context, error);
        });
        UserEntityImpl().getcountryInfoName().listen((data) {
          print("con $data");
          defultCity = data;
          cityLive.notifyView(data);
        }).onError((error) {
          print(error);
          Toast.debugShow(context, error);
        });
        UserEntityImpl().getprofilePic().listen((value) {
          print("head $value");
          headImgLive.notifyView(value);
        }).onError((error) {
          print(error);
          Toast.debugShow(context, error);
        });
      }
    });
  }

  void detectUpdate() {
    if (defultName != nameLive.data ||
        defultCity != cityLive.data ||
        selectImgPath != null) {
      saveLive.notifyView(true);
    } else {
      saveLive.notifyView(false);
    }
  }

  @override
  void dispose() {
    nameLive.dispost();
    headImgLive.dispost();
    cityLive.dispost();
    saveLive.dispost();
  }

  void jumpToCAM(BuildContext context, String pageName) {
    clickEvent(pageName, "quit");
    jumpToCamera(context, pageName, Api.PROVIDER_NAME, true, 1, null)
        .then((value) {
      if (value != null) {
        selectImgPath = value[0];
        headImgLive.notifyView(selectImgPath);
        detectUpdate();
      }
    }).catchError((error) {
      Toast.debugShow(context, error);
      print(error);
    });
  }

  void jumpToCTY(BuildContext context, String pageName) {
    jumpToCountry(context, pageName).then((value) {
      if (value != null) {
        cityLive.notifyView(value);
        detectUpdate();
      }
    }).catchError((error) {
      Toast.debugShow(context, error);
      print(error);
    });
  }

  void quit(BuildContext context, String pageName) {
    clickEvent(pageName, "quit");
  }
}
