//
//  CamelPrice.swift
//  驼峰价格显示组件
//
//  Created by Jason on 2024/1/23.
//

import SwiftUI

struct CamelPrice: View {
    @State public var amount: Double
    @State public var size = 16.0
    @State public var color = Color.red

    func getInt(amount: Double) -> String {
        return String(Int(amount.rounded(.down)))
    }

    func getDecimal(amount: Double) -> String {
        var decimal = String(amount.truncatingRemainder(dividingBy: 1))

        let range = decimal.range(of: "0.")!
        decimal.replaceSubrange(range, with: ".")

        if decimal.count < 3 {
            decimal.append("0")
        } else {
            decimal = String(decimal[..<decimal.index(decimal.startIndex, offsetBy: 3)])
        }
        return decimal
    }

    var body: some View {
        HStack(alignment: .bottom, spacing: 0) {
            Group {
                Text("￥")
                    .font(.system(size: size))
                    .baselineOffset(size / 10)
                    .offset(x: 2)
                Text(getInt(amount: amount))
                    .font(.system(size: size * 1.4))
                Text(getDecimal(amount: amount))
                    .font(.system(size: size))
                    .baselineOffset(size / 12)
            }
            .foregroundColor(color)
        }
    }
}
