
import Foundation
import SwiftUI

// MARK: - CalculatorKeyboard


extension String {
    var isNumber: Bool {
        let digitsCharacters = CharacterSet(charactersIn: "0123456789")
        return CharacterSet(charactersIn: self).isSubset(of: digitsCharacters)
    }
}


public struct CalculatorKeyboard: View {
    // MARK: Lifecycle
    @Binding var value: String
    @State var tempValue: String = ""
    
    public init(_ value: Binding<String>) {
        self._value = value
    }
    
    func onPressBack(){
        if (value.count > 0 ){
            value.removeLast()
        }
        
    }
    
    func onPressClear(key: String){
        value = ""
        tempValue = ""
    }
    
    func onPressEqual(key: String){
        if (tempValue == "") {
            return
        }
        if (tempValue.last?.isNumber == false){
            tempValue.removeLast()
        }
        let expression = NSExpression(format: tempValue)
        
        value = String(expression.expressionValue(with: nil, context: nil) as! Int)
        if (Int(value) ?? 0 < 0){
            value = ""
        }
    }
    
    func onPressKey(key:String){
        if (tempValue.last?.isNumber == false){
            if (key.isNumber == false){
                return
            }
        }
        var convertedKey = key
        if (key == "x") {convertedKey = "*"}
        if (key == "÷") {convertedKey = "/"}
        if (value == "" && (key == "0" || key == "000" || key.isNumber == false)) {
            return
        }
        else {
            value.append(key)
            tempValue.append(convertedKey)
        }
    }
    
    // MARK: Public
    
    public var body: some View {
        return GeometryReader { geo in
            VStack {
                HStack {
                    KeyboardButton("AC", action: self.onPressClear, color: Colors.pink07, textColor: Colors.black01)
                    KeyboardButton("÷", action: self.onPressKey, color: Colors.pink07, textColor: Colors.black01)
                    KeyboardButton("x", action: self.onPressKey, color: Colors.pink07, textColor: Colors.black01)
                    
                    SwiftUI.Button(action: onPressBack) {
                        HStack {
                            Image(
                                systemName: "delete.backward"
                            ).foregroundColor(Colors.black01)
                        }.frame(maxWidth: .infinity, minHeight: 48)
                            .background(Colors.pink07)
                            .foregroundColor(Colors.black01)
                            .clipShape(RoundedRectangle(cornerRadius: 8))
                    }
                }
                
                HStack {
                    KeyboardButton("7", action: self.onPressKey)
                    KeyboardButton("8", action: self.onPressKey)
                    KeyboardButton("9", action: self.onPressKey)
                    KeyboardButton("-", action: self.onPressKey, color: Colors.pink07, textColor: Colors.black01)
                }
                
                HStack {
                    KeyboardButton("4", action: self.onPressKey)
                    KeyboardButton("5", action: self.onPressKey)
                    KeyboardButton("6", action: self.onPressKey)
                    KeyboardButton("+", action: self.onPressKey, color: Colors.pink07, textColor: Colors.black01)
                }
                
                HStack{
                    VStack{
                        HStack {
                            KeyboardButton("1", action: self.onPressKey)
                            KeyboardButton("2", action: self.onPressKey)
                            KeyboardButton("3", action: self.onPressKey)
                        }
                        
                        HStack{
                            KeyboardButton("000", action: self.onPressKey)
                            KeyboardButton("0", action: self.onPressKey, width: CGFloat(geo.size.width * 0.25 - 5))
                        }
                    }
                    
                    KeyboardButton("=", action: self.onPressEqual, width: CGFloat(geo.size.width * 0.25 - 5), height: 102, color: Colors.primary, textColor: Colors.black01)
                    
                }
            }.background(Colors.background)
        }
    }
}
