/* eslint-disable import/prefer-default-export */
import {Utils} from 'tuya-panel-kit'
import {store} from '../models'
import I18n from '../i18n/index'
import dayjs from "dayjs";

export const getFaultStrings = (faultCode: string, faultValue: number, onlyPrior = true) => {
  const {devInfo} = store.getState()
  if (!faultValue) return ''
  const {label} = devInfo.schema[faultCode]
  const labels: string[] = []
  for (let i = 0; i < label!.length; i++) {
    const value = label![i]
    const isExist = Utils.NumberUtils.getBitValue(faultValue, i)
    if (isExist) {
      labels.push(I18n.getDpLang(faultCode, value))
      if (onlyPrior) break
    }
  }
  return onlyPrior ? labels[0] : labels.join(', ')
}

const RGB_MAX = 255
const HUE_MAX = 360
const SV_MAX = 100

export const hsv2Rgb = (h, s, v) => {
  if (typeof h === 'object') {
    const args = h
    h = args.h
    s = args.s
    v = args.v
  }

  h = _normalizeAngle(h)
  h = (h === HUE_MAX) ? 1 : (h % HUE_MAX / parseFloat(HUE_MAX.toString()) * 6)
  s = (s === SV_MAX) ? 1 : (s % SV_MAX / parseFloat(SV_MAX.toString()))
  v = (v === SV_MAX) ? 1 : (v % SV_MAX / parseFloat(SV_MAX.toString()))

  var i = Math.floor(h)
  var f = h - i
  var p = v * (1 - s)
  var q = v * (1 - f * s)
  var t = v * (1 - (1 - f) * s)
  var mod = i % 6
  var r = [v, q, p, p, t, v][mod]
  var g = [t, v, v, q, p, p][mod]
  var b = [p, p, t, v, v, q][mod]

  return {
    r: Math.floor(r * RGB_MAX),
    g: Math.floor(g * RGB_MAX),
    b: Math.floor(b * RGB_MAX),
  }
}

function _normalizeAngle(degrees) {
  return (degrees % 360 + 360) % 360
}

export const hex2Hsv = function (hex) {
  var rgb = hex2Rgb(hex)
  if (rgb) {
    return rgb2Hsv(rgb.r, rgb.g, rgb.b)
  } else {
    return null
  }

}

const hex2Rgb = function (hex) {
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16),
  } : null
}

export const rgb2Hsv = function (r, g, b) {
  if (typeof r === 'object') {
    const args = r
    r = args.r
    g = args.g
    b = args.b
  }

  // It converts [0,255] format, to [0,1]
  r = (r === RGB_MAX) ? 1 : (r % RGB_MAX / parseFloat(RGB_MAX.toString()))
  g = (g === RGB_MAX) ? 1 : (g % RGB_MAX / parseFloat(RGB_MAX.toString()))
  b = (b === RGB_MAX) ? 1 : (b % RGB_MAX / parseFloat(RGB_MAX.toString()))

  var max = Math.max(r, g, b)
  var min = Math.min(r, g, b)
  var h, s, v = max

  var d = max - min

  s = max === 0 ? 0 : d / max

  if (max === min) {
    h = 0 // achromatic
  } else {
    switch (max) {
      case r:
        h = (g - b) / d + (g < b ? 6 : 0)
        break
      case g:
        h = (b - r) / d + 2
        break
      case b:
        h = (r - g) / d + 4
        break
    }
    h /= 6
  }

  return {
    h: Math.round(h * HUE_MAX),
    s: Math.round(s * SV_MAX),
    v: Math.round(v * SV_MAX),
  }
}

export const rgb2Hex = function (r, g, b) {
  if (typeof r === 'object') {
    const args = r
    r = args.r
    g = args.g
    b = args.b
  }
  r = Math.round(r).toString(16)
  g = Math.round(g).toString(16)
  b = Math.round(b).toString(16)

  r = r.length === 1 ? '0' + r : r
  g = g.length === 1 ? '0' + g : g
  b = b.length === 1 ? '0' + b : b

  return '#' + r + g + b
}

export const hsv2Hex = function (h, s, v) {
  let rgb = hsv2Rgb(h, s, v)
  return rgb2Hex(rgb.r, rgb.g, rgb.b)
}

export const getHexByHSV = (hsv) => {
  const hueHex = hsv.h.toString(16).padStart(4, '0')
  const satHex = (hsv.s).toString(16).padStart(4, '0')
  const colorBrightnessHex = (hsv.v).toString(16).padStart(4, '0')
  const allHex = hueHex + satHex + colorBrightnessHex
  return allHex
}

export const getHSVByHex = (string) => {
  const h = string.substring(0, 4)
  const s = string.substring(4, 8)
  const v = string.substring(8, 12)
  return {
    h: parseInt(h, 16),
    s: parseInt(s, 16),
    v: parseInt(v, 16),
  }
}

export function mapFloatToRange(value: number, min: number, max: number): number {
  // 确保 value 在 [0, 1] 范围内
  value = Math.max(0, Math.min(1, value))

  // 计算插值后的值
  return min + value * (max - min)
}

export function mapValueToRatio(value: number, min: number, max: number): number {
  return ((value || 0) - min) / (max - min)
}

export function abbreviateMonths(str: string) {
  const monthAbbreviations = {
    January: 'Jan', February: 'Feb', March: 'Mar', April: 'Apr',
    May: 'May', June: 'Jun', July: 'Jul', August: 'Aug',
    September: 'Sep', October: 'Oct', November: 'Nov', December: 'Dec'
  };

  return str.replace(/(?:January|February|March|April|May|June|July|August|September|October|November|December)/g, match => monthAbbreviations[match]);
}

export function overDays(date: string, days: number): boolean {
  return dayjs().diff(dayjs(date), 'days') >= days
}
