import Vue from "vue";

import config from "../config";
import bus from "../utils/eventbus";
import { Alert, Confirm, Tips } from "../components/shared/message";

import clickoutside from "./v-clickoutside";
import clickwindow from "./v-clickwindow";
import * as api from "../api";

import { Message, InfiniteScroll,Button, Form,Input, FormItem, } from 'element-ui'

const MARERIAL = ["image", "pano", "scene", "audio", "video"];

const MAPTABLEHEADER = {};
MARERIAL.forEach((item) => {
  MAPTABLEHEADER[item] = require(`@/views/material/${item}/${item}`).data;
});

const MARERIALSTR = {
  image: "图片",
  pano: "全景图",
  scene: "场景",
  audio: "音频",
  video: "视频",
};

let STRSTATUS = {
  1: "制作中",
  2: "制作失败",
  3: "制作成功",
  4: "上传中",
  5: "上传失败"
};

Vue.use(InfiniteScroll)
Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input);

Vue.prototype.$bus = bus;
Vue.prototype.$api = api;
Vue.prototype.$intranet = config.intranet;
Vue.prototype.$cdn = config.CDN;
Vue.prototype.$config = config;
Vue.prototype.$thumb = config.thumb;
Vue.prototype.$noresult = config.noresult;
Vue.prototype.$MAPTABLEHEADER = MAPTABLEHEADER;
Vue.prototype.$MARERIALSTR = MARERIALSTR;
Vue.prototype.$STRSTATUS = STRSTATUS;
Vue.prototype.$msg = (data)=>{
  return Message({
    offset: 85,
    ...data
  });
}
Vue.prototype.$msg.success = (string)=>{
  return Message.success({
    offset: 85,
    message: string,
    duration: 1000
  });
}
Vue.prototype.$msg.warning = (string)=>{
  return Message.warning({
    offset: 85,
    message: string,
    duration: 1000
  });
}
Vue.prototype.$msg.message = (string)=>{
  return Message.info({
    offset: 85,
    message: string,
    duration: 1000
  });
}
Vue.prototype.$msg.error = (string)=>{
  return Message.error({
    offset: 85,
    message: string,
    duration: 1000
  });
}

Vue.prototype.$videoImg =
  "?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto";

Vue.prototype.$panoType = [
  {
    id: "building",
    name: "楼盘全景",
  },
  {
    id: "garden",
    name: "园林全景",
  },
  {
    id: "house",
    name: "户型",
  },
];

Vue.prototype.$nameStr = {
  building: "楼盘全景",
  garden: "园林全景",
  house: "户型",
};

Vue.prototype.$nameSort = {
  building: 1,
  garden: 2,
  house: 3,
};

Vue.prototype.$scrollbars = [];


let g_typeObj = {
  pcIcon: {
    acceptType:"image/png,image/jpeg",
    mediaType:"image",
    limit:1
  },
  appIcon:{
    acceptType:"image/png,image/jpeg",
    mediaType:"image",
    limit:1
  },
  bgm:{
    acceptType:"audio/mp3",
    mediaType:"audio",
    limit:20
  },
  image:{
    acceptType:"image/png,image/jpeg",
    mediaType:"image",
    limit:10,
    checkSuffix:['jpeg','jpg','png'],
    errMsg:`请上传10MB以内、jpg/png格式的图片`
  },
  video:{
    acceptType:"video/mp4",
    mediaType:"video",
    limit:200,
    checkSuffix:['mp4'],
    errMsg:`请上传200MB以内、mp4格式的视频`
  },
  audio: {
    acceptType:"audio/mp3",
    mediaType:"audio",
    limit: 20,
    checkSuffix:['audio'],
    errMsg:`请上传20MB以内、mp3格式的音频`
  },
}


let SettingPanel = require("@/framework/SettingPC").default;

Vue.mixin({
  components: {
    SettingPanel,
  },
  directives: {
    clickoutside: clickoutside,
    clickwindow: clickwindow,
  },
  data() {
    return {
      g_typeObj,
    };
  },
  methods: {
    $alert(data = {}) {
      let instance = new Alert({
        data,
      }).$mount();

      document.body.appendChild(instance.$el);

      Vue.nextTick(() => {
        instance.show = true;
      });
    },
    $confirm(data = {}) {
      let instance = new Confirm({
        data,
      }).$mount();

      document.body.appendChild(instance.$el);

      Vue.nextTick(() => {
        instance.show = true;
      });
    },
    $tips(data = {}) {
      let instance = new Tips({
        data,
      }).$mount();

      document.body.appendChild(instance.$el);

      Vue.nextTick(() => {
        instance.show = true;
      });
    },
    $isCacheId(id) {
      return String(id).indexOf("tmp") > -1;
    },
    $getStaticResource(url) {
      return config.getStaticResource(url);
    },
    $html_encode(str) {
      return config.html_encode(str);
    },
    $getKrpano(str) {
      return config.getKrpano(str);
    },
    $randomWord(randomFlag, min, max) {
      //随机字符串
      var str = "",
        range = min,
        arr = [
          "0",
          "1",
          "2",
          "3",
          "4",
          "5",
          "6",
          "7",
          "8",
          "9",
          "a",
          "b",
          "c",
          "d",
          "e",
          "f",
          "g",
          "h",
          "i",
          "j",
          "k",
          "l",
          "m",
          "n",
          "o",
          "p",
          "q",
          "r",
          "s",
          "t",
          "u",
          "v",
          "w",
          "x",
          "y",
          "z",
          "A",
          "B",
          "C",
          "D",
          "E",
          "F",
          "G",
          "H",
          "I",
          "J",
          "K",
          "L",
          "M",
          "N",
          "O",
          "P",
          "Q",
          "R",
          "S",
          "T",
          "U",
          "V",
          "W",
          "X",
          "Y",
          "Z",
        ];

      if (randomFlag) {
        // 随机长度
        range = Math.round(Math.random() * (max - min)) + min;
      }
      for (var i = 0; i < range; i++) {
        var pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
      }
      return str;
    },
    $unique(arr) {
      let map = new Map();
      let array = new Array(); // 数组用于返回结果
      for (let i = 0; i < arr.length; i++) {
        if (map.has(arr[i])) {
          // 如果有该key值
          map.set(arr[i], true);
        } else {
          map.set(arr[i], false); // 如果没有该key值
          array.push(arr[i]);
        }
      }
      return array;
    },
    $iosGrantedTips(cb) {
      var ua = navigator.userAgent.toLowerCase();
      if (ua.indexOf("like mac os x") > 0) {
        var reg = /os [\d._]*/gi;
        var verinfo = ua.match(reg);
        var version = (verinfo + "")
          .replace(/[^0-9|_.]/gi, "")
          .replace(/_/gi, ".");
        var arr = version.split(".");
        console.log(arr);
        
        if (arr[0] > 12 && arr[1] >= 0) {
          //对13.3以后的版本处理,包括13.3,
          DeviceMotionEvent && DeviceMotionEvent.requestPermission()
            .then((permissionState) => {
              if (permissionState == "granted") {
                cb({
                  msg:'授权成功',
                  code:1
                })
                this.reload();
              } else {
                cb({
                  msg:'授权失败',
                  code:0
                })
              }
            })
        } 
        else {
          //13.3以前的版本
          cb({
            msg:'无需授权，直接成功',
            code:1
          })
        }
      } else{
        cb({
          msg:'无需授权，直接成功',
          code:1
        })
      }
    },
    $htmlEncode(str) {
      var s = "";
      if (str.length == 0) return "";
      s = str.replace(/\'/g, "&#39;");
      s = s.replace(/\"/g, "&quot;");
      s = s.replace(/\(/g, "（");
      s = s.replace(/\)/g, "）");
      s = s.replace(/,/g, "，");
      return s;
    },
    $encodeStr: function (str, strv = '') {
      const NUM = 2
      const front = this.$randomWord(false, 8)
      const middle = this.$randomWord(false, 8)
      const end = this.$randomWord(false, 8)
  
      let str1 = str.substring(0, NUM)
      let str2 = str.substring(NUM)
  
      if (strv) {
        let strv1 = strv.substring(0, NUM)
        let strv2 = strv.substring(NUM)
        return [front + str2 + middle + str1 + end, front + strv2 + middle + strv1 + end]
      }
  
      return front + str2 + middle + str1 + end
    },
  },
});
