import Vue from 'vue';
const { getSystemInfoSync } = uni;

interface GlobalThis {
  $window: Object,
  $location: Object,
  $navigator: Object,
  $localStorage: Object,
  $sessionStorage: Object,
  $document: Object,
}

const $navigator = new Vue({
  data() {
    return {
      language: getSystemInfoSync().appLanguage || '',
      userAgent: 'miniprogram',
      geolocation: {
        getCurrentPosition(callback: Function, errorCallback: Function) {
          if (typeof errorCallback === 'function') {
            errorCallback({
              code: '-1',
              message: 'uniapp doesn`t support geolocation.getCurrentPosition().',
            });
          }
        },
      },
    };
  },
});


const $localStorage = new Vue({
  methods: {
    setItem(key: string, value: string) {
      uni?.setStorageSync(this.convertKey(key), value);
    },
    getItem(key: string) {
      if (uni) {
        return uni.getStorageSync(this.convertKey(key));
      }
      return undefined;
    },
    removeItem(key: string) {
      uni?.removeStorageSync(this.convertKey(key));
    },
    clear() {
      uni?.clearStorageSync();
    },
    convertKey(key: string) {
      if (key == null) {
        return key;
      }
      // @ts-ignore
      const currEnvVal = window.isTestNet ? 'test' : 'prod';
      return `${key}@${currEnvVal}`;
    },
  },
});

const $sessionStorage = new Vue({
  data() {
    return {
      sessionStorage: {},
    };
  },
  methods: {
    setItem(this: any, key: string, value: string) {
      this.sessionStorage[key] = value;
    },
    getItem(this: any, key: string) {
      return this.sessionStorage[key];
    },
    removeItem(this: any, key: string) {
      this.sessionStorage[key] = undefined;
    },
    clear() {
      this.sessionStorage = {};
    },
  },
});


const $body = new Vue({
  data() {
    return {
      clientWidth: getSystemInfoSync().windowWidth,
      clientHeight: getSystemInfoSync().windowHeight,
    };
  },
  methods: {
    addEventListener() {

    },
    removeEventListener() {

    },
    contains() {

    },
    removeChild() {

    },
  },
});

const $location = new Vue({
  data() {
    return {
      host: '',
      origin: '',
      hostname: '',
      pathname: '',
      protocol: 'https:',
    };
  },
  computed: {
    href: {
      set(this: any, newVal) {
        this.$router.push({ name: 'webview', query: { url: newVal } });
      },
      get() {
        const pages = getCurrentPages?.();
        if (!pages?.length) {
          return '';
        }
        const page = pages[pages.length - 1];
        // @ts-ignore
        return page?.$page?.fullPath || '';
      },
    },
  },
  methods: {
    reload() {
      // refreshCurrentPage();
    },
  },
});

const $document = new Vue({
  data() {
    return {
      location: $location,
      body: $body,
    };
  },
  computed: {
    cookie: {
      set(newVal: string) {
        $localStorage.setItem('uni-app-cookie', newVal);
      },
      get() {
        return $localStorage.getItem('uni-app-cookie') || '';
      },
    },
  },
  methods: {
    querySelector() {

    },
    createElement() {

    },
    addEventListener() {

    },
    removeEventListener() {

    },
  },
});

const $window = new Vue({
  data() {
    return {
      app: null,
      document: $document,
      location: $location,
      navigator: $navigator,
      devicePixelRatio: getSystemInfoSync().pixelRatio,
      screen: {
        width: getSystemInfoSync().screenWidth,
        height: getSystemInfoSync().screenHeight,
      },
      Event: {},
      localStorage: $localStorage,
      sessionStorage: $sessionStorage, /* 其他环境没有sessionStorage，如果确实有需要，可以自己想办法加个运行时key前缀来实现*/
      isTestNet: (process.env.VUE_APP_NET_ENV === 'test'),
      userInfo: {
        ticket: '', // 小程序票据
        ticketExpireTime: '', // 小程序票据过期时间
        targetOpenid: '', // gameOpenid
        uType: 2, // 登录类型，1：QQ， 2：微信
        targetAppid: '',  // 业务目标Appid，一般为游戏appid
      },
    };
  },
  methods: {
    addEventListener(eventName: string) {
      console.log('uniapp不支持该方法：addEventListener', eventName);
    },
    removeEventListener() {
      console.log('uniapp不支持该方法：removeEventListener');
    },
  },
});

(globalThis as unknown as GlobalThis).$window = $window;
(globalThis as unknown as GlobalThis).$location = $window.location;
(globalThis as unknown as GlobalThis).$navigator = $window.navigator;
(globalThis as unknown as GlobalThis).$localStorage = $window.localStorage;
(globalThis as unknown as GlobalThis).$sessionStorage = $window.sessionStorage;
(globalThis as unknown as GlobalThis).$document = $document;

console.info('当前网络环境:', $window.isTestNet ? '测试环境' : '正式环境');

export default $window;
