/*
 * @author lsy
 * @date   2019-09-24
 **/
import 'package:flutter/services.dart';
import 'package:rxdart/rxdart.dart';

const platform = const MethodChannel('flutter_bury_channel');

var startTime;

Observable<bool> pageStart(String nowPage, String beforPage) {
  startTime=getNowTimeSecond();
  return Observable.fromFuture(platform.invokeMethod(
      "PAGE_START", {"page_name": nowPage, "referrer": beforPage}));
}

Observable<bool> pageEnd(String nowPage, String beforPage) {
  return Observable.fromFuture(platform.invokeMethod(
      "PAGE_END", {"page_name": nowPage, "referrer": beforPage,
    "in": startTime==null?DateTime.now().millisecondsSinceEpoch:startTime,
    "out": getNowTimeSecond()}));
}

Observable<bool> click(String page_name, String button_name) {
  return Observable.fromFuture(platform.invokeMethod(
      "CLICK_EVENT", {"page_name": page_name, "button_name": button_name}));
}

Observable<bool> jumpToH5(
    String nowPage, String beforPage, String button_name) {
  return Observable.fromFuture(platform.invokeMethod("FLUTTER_TO_H5", {
    "page_name": nowPage,
    "referrer": beforPage,
    "in": startTime==null?DateTime.now().millisecondsSinceEpoch:startTime,
    "out": getNowTimeSecond(),
    "button_name": button_name
  }));
}

Observable<bool> jumpToPictorial(String url, String nowPage, String beforPage,
     String button_name) {
  return Observable.fromFuture(platform.invokeMethod("FLUTTER_TO_PICTORIAL", {
    "url": url,
    "page_name": nowPage,
    "referrer": beforPage,
    "in": startTime==null?DateTime.now().millisecondsSinceEpoch:startTime,
    "out": getNowTimeSecond(),
    "button_name": button_name
  }));
}

Observable<bool> jumpToSearch(
    String nowPage, String beforPage, String button_name) {
  return Observable.fromFuture(platform.invokeMethod("FLUTTER_TO_SEARCH", {
    "page_name": nowPage,
    "referrer": beforPage,
    "in": startTime==null?DateTime.now().millisecondsSinceEpoch:startTime,
    "out": getNowTimeSecond(),
    "button_name": button_name
  }));
}

String getNowTimeSecond() {
  return "${DateTime.now().millisecondsSinceEpoch}";
//  String year = "${DateTime.now().year}".length == 4
//      ? "${DateTime.now().year}"
//      : "0${DateTime.now().year}";
//  String month = "${DateTime.now().month}".length == 2
//      ? "${DateTime.now().month}"
//      : "0${DateTime.now().month}";
//  String day = "${DateTime.now().day}".length == 2
//      ? "${DateTime.now().day}"
//      : "0${DateTime.now().day}";
//  String hour = "${DateTime.now().hour}".length == 2
//      ? "${DateTime.now().hour}"
//      : "0${DateTime.now().hour}";
//  String minute = "${DateTime.now().minute}".length == 2
//      ? "${DateTime.now().minute}"
//      : "0${DateTime.now().minute}";
//  String second = "${DateTime.now().second}".length == 2
//      ? "${DateTime.now().second}"
//      : "0${DateTime.now().second}";
//
//  return "${year}${month}${day}"
//      "${hour}${minute}${second}";
}
