import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gmalpha_flutter/base/user_tool.dart';
import 'package:gmalpha_flutter/model/setting_model.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AppConfig<T extends ChangeNotifier> {
  static const primaryColor = Color(0xFF5DBE82); //主题色

  static const disabledMainColor = Color.fromRGBO(97, 190, 130, 0.5);
  static const textColor = Color(0xFF333333);
  static const grayTextColor = Color(0xFF71747E);
  static const backgroundColor = Color(0xFFF5F5F8);
  static const divider = Color(0xFFdddddd);

  static Color hexToColor(String code) {
    return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
  }

  static Widget getPlaceHoder([width, height]) {
    return new Container(
        width: width,
        height: height,
        child: new Image.asset(
          'images/placehoder_img.png',
          fit: BoxFit.cover,
        ));
  }

  static Widget getUserPlaceHoder(width, height) {
    return new Container(
        width: width,
        height: height,
        child: new Image.asset('images/nologin.png'));
  }

  static Widget getLoadingPlaceHoder(width, height) {
    return new SizedBox(
        width: width,
        height: height,
        child: const CircularProgressIndicator(strokeWidth: 2.0));
  }

  //debug:false  release: true
  static const bool inProduction =
      const bool.fromEnvironment("dart.vm.product");

  static Widget initLoading(bool showEmpty, [String emptyText = '暂无数据']) {
    return new Center(
      child: showEmpty
          ? _initEmpty(emptyText)
          : SizedBox(
              width: 35.0,
              height: 35.0,
              child: const CircularProgressIndicator(strokeWidth: 2.0)),
    );
  }

  static Widget _initEmpty(String emptyText) {
    return new Container(
      padding: EdgeInsets.fromLTRB(0, 80, 0, 0),
      child: new Column(
        children: <Widget>[
          new Icon(
            Icons.hourglass_empty,
            color: Colors.grey,
            size: 60,
          ),
          new Container(
            height: 10,
          ),
          new Text(emptyText)
        ],
      ),
    );
  }

  static SharedPreferences _spf;

  static List<SingleChildCloneableWidget> list = [
    ChangeNotifierProvider<SettingProvide>.value(value: SettingProvide()),
  ];

  static init() async {

  }

}

const bool inProduction = const bool.fromEnvironment("dart.vm.product");
const BASEURL_RELEASE = 'https://api.github.com/';
const BASEURL_DEBUG = 'https://api.github.com/';

class ApiDio {
  static ApiDio instance;
  static ApiService apiService;

  ApiDio() {
    var options = new BaseOptions(
        connectTimeout: 5000,
        receiveTimeout: 3000,
        baseUrl: inProduction ? BASEURL_RELEASE : BASEURL_DEBUG,
        contentType: new ContentType('application', 'x-www-form-urlencoded',
            charset: 'utf-8'));
    Dio dio = new Dio(options);
    dio.interceptors.add(AuthInterceptor()); // 添加 token
    dio
      ..interceptors
          .add(LogInterceptor(responseBody: true, requestBody: true)); //添加日志
    apiService = new ApiService(dio);
  }

  static ApiService getInstance() {
    if (instance == null) {
      instance = new ApiDio();
    }
    return _getApiService();
  }

  static ApiService _getApiService() {
    return apiService;
  }
}

class AuthInterceptor extends Interceptor {
  String PLATFORM = "android";
  String CLIENT = "store";

  @override
  onRequest(RequestOptions options) async {
    Map<String, String> headers = new Map();
    headers["Accept-Charset"] = "utf-8";
    headers["Connection"] = "keep-alive";
    headers["Accept"] = "*/*";
    headers["x-version"] = "XXX";
    headers["x-platform"] = PLATFORM;
    headers["x-client"] = CLIENT;
    headers["x-equCode"] = "XXX";
    headers["Authorization"] = "XXX";
    headers["token"] = "Xxx";

    options.headers = headers;
    return super.onRequest(options);
  }
}

class ApiService {
  ApiService(this.dio);

  Dio dio;

  Future _get(String url, {Map<String, dynamic> params}) async {
    var response = await dio.get(url, queryParameters: params);
    return response.data;
  }

  Future _post(String url, Map<String, dynamic> params) async {
    var response = await dio.post(url, data: params);
    return response.data;
  }

  Observable post(String url, Map<String, dynamic> params) =>
      Observable.fromFuture(_post(url, params)).asBroadcastStream();

  Observable get(String url, {Map<String, dynamic> params}) =>
      Observable.fromFuture(_get(url, params: params)).asBroadcastStream();
}