//
//  SwiftUI_Modifier.swift
//  PersonalHealthDiary
//
//  Created by Sean Kim on 2/20/24.
//

import SwiftUI


extension View {
    func makeSystemButton(_ systemName: String, text: String = "",color: Color = .black, img:CGFloat = 40,size: CGFloat = 20) -> some View {
        modifier(BottomModifier(systemName: systemName,color: color,text: text, fontSize: size, imgSize: img))
    }
    
    func fullPage(_ backColor: Color) -> some View  {
        self
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(backColor)
    }
    
    func font(_ type: FontType)-> some View {
        switch type {
        case .Title:
            return AnyView(self.modifier(TitleFont()))
        case .Content:
            return AnyView(self.modifier(ContentFont()))
        case .Small:
            return AnyView(self.modifier(SmallFont()))
        }
    }
}
#if canImport(UIKit)
extension View {
    func hideKeyboard() {
            UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        }
}
#endif

struct BottomModifier: ViewModifier {
    var systemName: String
    var color: Color
    var text: String
    var fontSize: CGFloat
    var imgSize: CGFloat
    
    func body(content: Content) -> some View {
        VStack {
            Image(systemName: systemName)
                .resizable().aspectRatio(contentMode: .fill)
                .tint(color)
                .frame(width: imgSize ,height: imgSize)
                .padding(.init(top: 0, leading: 10, bottom: 0, trailing: 10))
            if text != "" {
                Text(text)
                    .tint(color)
                    .font(.system(size: fontSize))
                    .padding(.init(top: 0, leading: 0, bottom: 5, trailing: 0))
            }
        }
    }
}


struct MenuButtonBack: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.nps(size: 24))
            .tint(.brand)
            .frame(width: 40, height: 40, alignment: .center)
            .background{
                RoundedRectangle(cornerRadius: 10)
                    .strokeBorder(.brand,
                            style: StrokeStyle(lineWidth: 2))
            }
            
    }
}

struct TitleFont: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.nps(font: .bold, size: 32))
            .minimumScaleFactor(0.1)
            .tint(.brand)
            .frame(maxHeight: 32, alignment: .leading)
    }
}

// Title 보다 40% 감소
struct ContentFont: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.nps(size: 20))
            .minimumScaleFactor(0.1)
            .tint(.brand)
            .frame(maxHeight: 20, alignment: .leading)
    }
}

// Content 보다 30% 감소
struct SmallFont: ViewModifier {
    func body(content: Content) -> some View {
        content
            .font(.nps(size: 14))
            .minimumScaleFactor(0.1)
            .tint(.brand)
            .frame(maxHeight: 14, alignment: .leading)
    }
}

struct AlignmentView: ViewModifier {
    enum AlignmentType {
        case leading
        case trailing
        case top
        case bottom
    }
    
    var type: AlignmentType
    
    func body(content: Content) -> some View {
        if type == .leading || type == .trailing {
            HStack(spacing: 0) {
                if type == .trailing { Spacer() }
                content
                if type == .leading { Spacer() }
            }
        }
        else {
            VStack(spacing: 0) {
                if type == .bottom { Spacer() }
                content
                if type == .top { Spacer() }
            }
        }
    }
}
