import React  from 'react'
import seedrandom from "seedrandom"
import CryptoJS from "crypto-js"
import FileSaver from "file-saver"
import * as XLSX from "xlsx"
import { pdf } from '@react-pdf/renderer';
import moment from "moment"

import DefaultTemplateDocument from './pdf-templates/default-template'
import {MettaSvr_Common} from '../../redux/helpers/action-enums'

const convertToRGB = function(theColor){
    var aRgbHex = theColor.match(/.{1,2}/g)
    var aRgb = [
        parseInt(aRgbHex[0], 16),
        parseInt(aRgbHex[1], 16),
        parseInt(aRgbHex[2], 16)
    ]
    return aRgb
}


export const encryptURIparams = (sourceString) => {
    const encryptedString = CryptoJS.AES.encrypt(sourceString, MettaSvr_Common.CIPHER_DEV_KEY).toString()
    return encodeURIComponent(encryptedString)
}
//https://dev.to/jasurkurbanovinit/how-to-export-data-to-excel-from-api-using-react-25go
export const exportToExcel = ({ apiData, fileName }) => {
    const fileType =
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"
    const fileExtension = ".xlsx"
  
    const ws = XLSX.utils.json_to_sheet(apiData)
    const wb = { Sheets: { data: ws }, SheetNames: ["data"] }
    const excelBuffer = XLSX.write(wb, { bookType: "xlsx", type: "array" })
    const data = new Blob([excelBuffer], { type: fileType })
    FileSaver.saveAs(data, fileName + fileExtension)

}

export const exportToPdf = async ({ apiData, fileName, template=<DefaultTemplateDocument data={apiData}/>}) => {

    const fileExtension = ".pdf"
    const blob = await pdf(template).toBlob()
    FileSaver.saveAs(blob, fileName + fileExtension)

}

export const decryptURIparams = (sourceString) => {
    if(!!sourceString){
        // Decrypt
        const decodedCipherString = decodeURIComponent(sourceString)
        var bytes  = CryptoJS.AES.decrypt(decodedCipherString, MettaSvr_Common.CIPHER_DEV_KEY)
        var destinationString = bytes.toString(CryptoJS.enc.Utf8)
        return destinationString
    }else{
        return null
    }
}

export const getRandomColorGenerated = (seeder, alpha = 1) => {
    var rng = seedrandom(seeder)
    var randomColor = Math.floor(rng() * 16777215).toString(16)
    // console.log(seeder, randomColor)
    const colorBits = convertToRGB(randomColor)
    var r = colorBits[0]
    var g = colorBits[1]
    var b = colorBits[2]
    var color = `rgba(${r}, ${g}, ${b}, ${alpha})`;
    // console.log(color)
    return color
}

/**
 * 
 * @param {*} weight of the person 
 * @param {*} height of the person
 * @returns 
 */
export const bmiCalculation = (weight, height) =>  height > 0 ? (weight / (height * height)) : 0

export const isTodayOrYesterdayOrDayBeforeYesterday = (date) => {
    const today = moment().add(1, 'days').format(MettaSvr_Common.SaveDateFormat)
    const dayBeforeYesterday = moment().add(-3, 'days').format(MettaSvr_Common.SaveDateFormat)
    return moment(date).isBetween(dayBeforeYesterday, today)
  }

export  const notAfterToday = (date) => {
    const today = moment()
    return today.isAfter(date)
  }
