// OpenSTA, Static Timing Analyzer
// Copyright (c) 2026, Parallax Software, Inc.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// 
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
// 
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 
// This notice may not be removed or altered from any source distribution.

#include "Units.hh"

#include <cmath> // abs

#include "Format.hh"
#include "StringUtil.hh"
#include "MinMax.hh"  // INF
#include "Fuzzy.hh"

namespace sta {

Unit::Unit(const char *suffix) :
  scale_(1.0),
  suffix_(suffix),
  digits_(3)
{
  setScaleAbbrevSuffix();
}

Unit::Unit(float scale,
           const char *suffix,
           int digits) :
  scale_(scale),
  suffix_(suffix),
  digits_(digits)
{
  setScaleAbbrevSuffix();
}

void
Unit::setScaleAbbrevSuffix()
{
  scale_abbrev_suffix_ = scaleAbbreviation() + suffix_;
}

double
Unit::staToUser(double value)
{
  return value / scale_;
}

double
Unit::userToSta(double value)
{
  return value * scale_;
}

void
Unit::setScale(float scale)
{
  scale_ = scale;
  setScaleAbbrevSuffix();
}

std::string
Unit::scaleAbbreviation() const
{
  if (fuzzyEqual(scale_, 1E+6))
    return "M";
  else if (fuzzyEqual(scale_, 1E+3))
    return "k";
  if (fuzzyEqual(scale_, 1.0))
    return "";
  else if (fuzzyEqual(scale_, 1E-3))
    return "m";
  else if (fuzzyEqual(scale_, 1E-6))
    return "u";
  else if (fuzzyEqual(scale_, 1E-9))
    return "n";
  else if (fuzzyEqual(scale_, 1E-12))
    return "p";
  else if (fuzzyEqual(scale_, 1E-15))
    return "f";
  else
    return "?";
}

std::string
Unit::scaleString() const
{
  if (fuzzyEqual(scale_, 1E+6))
    return "1M";
  else if (fuzzyEqual(scale_, 1E+3))
    return "1k";
  if (fuzzyEqual(scale_, 1.0))
    return "1";
  else if (fuzzyEqual(scale_, 1E-3))
    return "1m";
  else if (fuzzyEqual(scale_, 1E-6))
    return "1u";
  else if (fuzzyEqual(scale_, 1E-9))
    return "1n";
  else if (fuzzyEqual(scale_, 1E-12))
    return "1p";
  else if (fuzzyEqual(scale_, 1E-15))
    return "1f";
  else
    return sta::format("{:.1e}", scale_);
}

std::string
Unit::scaleSuffix() const
{
  return scaleString() + suffix_;
}

void
Unit::setSuffix(const char *suffix)
{
  suffix_ = suffix;
  setScaleAbbrevSuffix();
}

void
Unit::setDigits(int digits)
{
  digits_ = digits;
}

int
Unit::width() const
{
  return digits_ + 2;
}

std::string
Unit::asString(float value) const
{
  return asString(value, digits_);
}

std::string
Unit::asString(float value,
               int digits) const
{
  // Special case INF because it blows up otherwise.
  if (std::abs(value) >= INF * .1)
    return (value > 0.0) ? "INF" : "-INF";
  else {
    float scaled_value = value / scale_;
    // prevent "-0.00" on slowaris
    if (std::abs(scaled_value) < 1E-6)
      scaled_value = 0.0;
    return sta::formatRuntime("{:.{}f}", scaled_value, digits);
  }
}

////////////////////////////////////////////////////////////////

Units::Units() :
  time_unit_("s"),
  resistance_unit_("ohm"),
  capacitance_unit_("F"),
  voltage_unit_("v"),
  current_unit_("A"),
  power_unit_("W"),
  distance_unit_("m"),
  scalar_unit_("")
{
}

Unit *
Units::find(std::string_view unit_name)
{
  if (stringEqual(unit_name, "time"))
    return &time_unit_;
  else if (stringEqual(unit_name, "resistance"))
    return &resistance_unit_;
  else if (stringEqual(unit_name, "capacitance"))
    return &capacitance_unit_;
  else if (stringEqual(unit_name, "voltage"))
    return &voltage_unit_;
  else if (stringEqual(unit_name, "current"))
    return &current_unit_;
  else if (stringEqual(unit_name, "power"))
    return &power_unit_;
  else if (stringEqual(unit_name, "distance"))
    return &distance_unit_;
  else
    return nullptr;
}

Units &
Units::operator=(const Units &units)
{
  time_unit_ = *units.timeUnit();
  resistance_unit_ = *units.resistanceUnit();
  capacitance_unit_ = *units.capacitanceUnit();
  voltage_unit_ = *units.voltageUnit();
  current_unit_ = *units.currentUnit();
  power_unit_ = *units.powerUnit();
  distance_unit_ = *units.distanceUnit();
  scalar_unit_ = *units.scalarUnit();
  return *this;
}

} // namespace sta
