/*
 * @author lsy
 * @date   2019-10-15
 **/
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:gmalpha_flutter/commonModel/GMBase.dart';
import 'package:gmalpha_flutter/commonModel/toast/toast.dart';
import 'package:gmalpha_flutter/userModel/page/country/CountryJson.dart';
import 'package:gmalpha_flutter/userModel/service/UserRepository.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/CountryBean.dart';

class CountryModel extends BaseModel {
  LiveData<CountryBean> liveData = new LiveData();

  final String refer;
  String selectCity;

  CountryModel(this.refer);

  getCountries(BuildContext context) {
    getStaticCountry().then((value) {
      if (value != null) {
        liveData.notifyView(value);
      } else {
        getCountryFromNet(context);
      }
    }).catchError((error) {
      Toast.debugShow(context, error);
      print(error);
    });
  }

  getCountryFromNet(BuildContext context) {
    UserRepository.getInstance().getCountries().listen((value) {
      liveData.notifyView(value);
    }).onError((error) {
      Toast.debugShow(context, error);
      print(error);
    });
  }

  Future getStaticCountry() async {
    return await CountryBean.fromJson(json.decode(STATIC_COUNTRY_JSON));
  }

  @override
  void dispose() {
    liveData.dispost();
  }
}
