import config from '@/config'
import uma from 'umtrack-wx'

function umaInit() {
  config.appKey &&
    uma.init({
      appKey: config.appKey, // 由友盟分配的APP_KEY
      // 使用Openid进行统计，此项为false时将使用友盟+uuid进行用户统计。
      // 使用Openid来统计微信小程序的用户，会使统计的指标更为准确，对系统准确性要求高的应用推荐使用Openid。
      useOpenid: false,
      // 使用openid进行统计时，是否授权友盟自动获取Openid，
      // 如若需要，请到友盟后台"设置管理-应用信息"(https://mp.umeng.com/setting/appset)中设置appId及secret
      autoGetOpenid: false
      // debug: true, // 是否打开调试模式
      // uploadUserInfo: true // 自动上传用户信息，设为false取消上传，默认为false
    })
}

const SliderUnit = 0.5 // 一个色块代表0.5个小时
const TimeStart = 0
const TimeEnd = 24

function generateTimeSliders(openStartTime, openEndTime, disabledPast = true) {
  let start = TimeStart,
    end = TimeEnd
  let duration = end - start
  let times = []
  let { juggeValue } = getNowTime()
  for (let i = 0; i < duration / SliderUnit; i++) {
    let value = start + i * SliderUnit
    let values = value.toString().split('.')
    let status = ''
    let label = ''
    if (values.length == 1) {
      // 代表是整数
      label =
        parseInt(values[0]) < 10 ? '0' + values[0] + ':00' : values[0] + ':00'
    } else {
      // 代表是有分钟
      let mniuts = SliderUnit * 60 // 转化分钟;
      label =
        parseInt(values[0]) < 10
          ? '0' + values[0] + ':' + mniuts
          : values[0] + ':' + mniuts
    }
    if (disabledPast && value < juggeValue) {
      status = 'past' // 过去时间
    }

    if (openEndTime && value >= openStartTime && value <= openEndTime) {
      status = 'unOpened' // 不在开放时间内
    }

    times.push({
      value,
      label,
      status
    })
  }
  return times
}
function getNowTime() {
  let now = new Date()
  let hour = now.getHours()
  let minuts = now.getMinutes()
  // let hour = 16;
  // let minuts = 30;
  let minutsValue = 0
  if (minuts < 30) {
    // 在30分钟内，都可以预约当前小时，故取0
    minutsValue = 0
  } else {
    minutsValue = 0.5 // 在30分钟-59分钟内，都可以预约当前小时+半小时之后，故取0.5
  }
  let juggeValue = hour + minutsValue

  return {
    juggeValue,
    trueValue: {
      hour,
      minuts
    }
  }
}

function getNowDate(date) {
  let now = new Date()
  return getDate(now)
}

function getDate(now) {
  let year = now.getFullYear()
  let month = now.getMonth() + 1
  let date = now.getDate()
  return [year, getTowNum(month), getTowNum(date)].join('')
}

function getTowNum(num) {
  num = parseInt(num)
  return num < 10 ? '0' + num : num
}

function formateTimeValue(timeValue) {
  let value = parseFloat(timeValue)
  let hour = parseInt(timeValue)
  let minits = (value - hour) * 60
  return `${getTowNum(hour)}${getTowNum(minits)}00`
}

function formateDateValue(dateValue) {
  return dateValue.replace(/-/g, '')
}

function formateTimeToValue(time) {
  if (time === '') return 0
  let hours = time.substring(0, 2)
  let mins = time.substring(2, 4)
  let hoursValue = parseInt(hours)
  let mintusValue = parseInt(mins) / 60
  return hoursValue + mintusValue
}

function formateTimeToColon(time) {
  let hours = time.substring(0, 2)
  let mins = time.substring(2, 4)
  return `${hours}:${mins}`
}

function endtimeToUnitTime(time) {
  let hours = time.substring(0, 2)
  let mins = time.substring(2, 4)
  if (mins < 30) {
    mins = '00'
  } else {
    mins = '30'
  }
  return `${hours}${mins}00`
}
function startimeToUnitTime(time) {
  let hours = time.substring(0, 2)
  let mins = time.substring(2, 4)
  if (mins < 30 && mins !== '00') {
    mins = '30'
  } else if (mins > 30) {
    mins = '00'
    hours = parseInt(hours) + 1
    hours = hours < 10 ? `0${hours}` : hours
  } else {
  }
  return `${hours}${mins}00`
}

function formateBookedList(bookedList) {
  let list = {}
  if (!bookedList || (bookedList && bookedList.length == 0)) {
    return list
  }

  bookedList.forEach((item) => {
    let { startTime, endTime, startDate } = item
    let key = startDate
    let data = {
      start: formateTimeToValue(endtimeToUnitTime(startTime)),
      end: formateTimeToValue(startimeToUnitTime(endTime)),
      ...item
    }
    if (!list[key]) {
      list[key] = [data]
    } else {
      list[key].push(data)
    }
  })
  return list
}

function dateToDate(val) {
  const year = val.slice(0, 4)
  const month = val.slice(4, 6)
  const day = val.slice(6, 8)
  return `${year}-${month}-${day}`
}

function getWeekDay(date) {
  let week = date.getDay()
  let weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return weekday[week]
}
function checkSpaceControl(val) {
  if (!val) {
    wx.showToast({
      title: '您没有权限！',
      icon: 'none',
      duration: 1500
    })
    return false
  } else {
    return true
  }
}

function carbonCredit(projectId) {
  return '碳积分'
}

function carbonCreditExplain(params) {
  const arr = [
    {
      id: '',
      sty: '',
      text: '空调加时积分规则：'
    },
    {
      id: '1.',
      sty: '',
      text: '空调/照明加时申请，增加积分1.5；'
    },
    {
      id: '2.',
      sty: '',
      text: '空调/照明加时时段内，点击“我要离开”主动结束加时，增加积分0.5。'
    },
    {
      id: '',
      sty: '',
      text: '空调&照明操作积分规则：'
    },
    {
      id: '1.',
      sty: '',
      text: '当日主动关闭空调或照明设备，且持续时间大于1小时，空调积2分，照明积0.3分；'
    },
    {
      id: '2.',
      sty: '',
      text: '夏季，空调调节温度大于等于26℃，且持续时间大于30分钟，积0.3分；'
    },
    {
      id: '3.',
      sty: '',
      text: '冬季，空调调节温度小于等于21℃，且持续时间大于30分钟，积0.3分；'
    },
    {
      id: '4.',
      sty: '',
      text: '办公室出现大于等于2个空调或照明设备时，用户开启空调或照明30分钟后，未开启的空调或照明数量大于等于1个时，空调积1分，照明积0.2分。'
    },
    {
      id: '',
      sty: '',
      text: '会议场景积分规则：'
    },
    {
      id: '1.',
      sty: '',
      text: '会议期间提前结束会议，从讨论或投影场景切换至散会场景时，积2分。'

    },
    {
      id: '',
      sty: 'color:#9FA3AF;padding-top:20rpx;',
      text: '说明：'
    },
    {
      id: '1.',
      sty: 'color:#9FA3AF;font-size:24rpx',
      text: '以上低碳行为会根据系统规则在正常使用空调/照明等设备的合理范围内限制积分次数；'
    },
    {
      id: '2.',
      sty: 'color:#9FA3AF;font-size:24rpx',
      text: '低碳积分将同时计入个人和企业积分账户。'
    }

  ]
  return arr
}

export {
  carbonCreditExplain,
  carbonCredit,
  checkSpaceControl,
  umaInit,
  generateTimeSliders,
  getNowTime,
  formateBookedList,
  formateTimeValue,
  formateTimeToValue,
  formateDateValue,
  getNowDate,
  formateTimeToColon,
  dateToDate,
  getWeekDay,
  endtimeToUnitTime,
  startimeToUnitTime,
  TimeEnd,
  TimeStart,
  SliderUnit
}
