//
//  ShapesView.swift
//  SwiftUICatalog
//
// MIT License
//
// Copyright (c) 2021 Barbara Martina Rodeker
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//

import SwiftUI

///
/// Examples on how to use  SHAPES  in SwiftUI
/// OFFICIAL DOCUMENTATION:
/// https://developer.apple.com/documentation/swiftui/shape
/// https://developer.apple.com/documentation/swiftui/insettableshape
/// https://developer.apple.com/documentation/swiftui/rectangle
/// https://developer.apple.com/documentation/swiftui/roundedrectangle
/// https://developer.apple.com/documentation/swiftui/circle
/// https://developer.apple.com/documentation/swiftui/ellipse
/// https://developer.apple.com/documentation/swiftui/capsule
/// https://developer.apple.com/documentation/swiftui/path
///
/// https://developer.apple.com/documentation/swiftui/scaledshape
/// https://developer.apple.com/documentation/swiftui/rotatedshape
/// https://developer.apple.com/documentation/swiftui/offsetshape
/// https://developer.apple.com/documentation/swiftui/transformedshape
/// https://developer.apple.com/documentation/swiftui/containerrelativeshape
/// 
///

struct ShapesView: View {
    var body: some View {
        
        
        ScrollView {
            
            // MARK: - rectangle
            
            Group {
                HeaderView(title: "Rectangle")
                Rectangle()
                    .fill(Color.yellow)
                    .frame(width: 100, height: 100)
            }
            
            // MARK: - Circle
            
            Group {
                HeaderView(title: "Circle")
                Circle()
                    .fill(Color.yellow)
                    .frame(width: 100, height: 100)
            }
            
            // MARK: - Ellipse
            
            Group {
                HeaderView(title: "Ellipse")
                Ellipse()
                    .fill(Color.yellow)
                    .frame(width: 100, height: 200)
            }
            
            // MARK: - capsule
            
            Group {
                HeaderView(title: "Capsule")
                Capsule()
                    .fill(Color.yellow)
                    .frame(width: 100, height: 150)

            }
            
            // MARK: - rounded rectangle
            
            Group {
                HeaderView(title: "Rounded Rectangle")
                RoundedRectangle(cornerSize: CGSize(width: 10, height: 10))
                    .fill(Color.yellow)
                    .frame(width: 100, height: 100)
            }
            
            // MARK: - custom
            
            Group {
                HeaderView(title: "Custom shape")
                CustomShape()
                    .fill(Color.yellow)
                    .frame(width: 100, height: 100)
            }

            // MARK: - insetable shape
            
            Group {
                HeaderView(title: "Insettable shape")
                Rectangle()
                    .strokeBorder(style: StrokeStyle(lineWidth: 5,
                                                     lineCap: CGLineCap.round,
                                                     lineJoin: CGLineJoin.miter,
                                                     miterLimit: 2,
                                                     dash: [1, 2],
                                                     dashPhase: 4))
                    .frame(width: 100, height: 100)
            }


        }
        // end of scroll view
        .padding()
        
    }
}

struct ShapesView_Previews: PreviewProvider {
    static var previews: some View {
        ShapesView()
    }
}

struct CustomShape: Shape {
    
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        let radius = min(rect.midX, rect.midY)
        
        path.addArc(
            center: CGPoint(x: rect.midX, y: rect.midY),
            radius: radius,
            startAngle: Angle(degrees: 0),
            endAngle: Angle(degrees: 180),
            clockwise: true)
        
        path.addLine(to: CGPoint(x: rect.minX, y: rect.minY))
        
        path.closeSubpath()
        
        return path
    }
}
