//
//  IconView.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 2/13/22.
//

import SwiftUI

struct IconView: View {
    @State public var customIcon: CustomIcon
    
    private let facePercSize = 0.6
    public let isPreview: Bool
    
    private var gridXOffset: CGFloat {
        if isPreview {
            return CGFloat(0)
        }
        return CGFloat(6)
    }
    
    private var gridYOffset: CGFloat {
        if isPreview {
            return CGFloat(0)
        }
        return CGFloat(-8)
    }
    
    private var entireFuckingViewOffset: CGFloat {
        if isPreview {
            return CGFloat(0)
        }
        return CGFloat(25)
    }
    
    let columns = [
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1),
        GridItem(.flexible(minimum: 1, maximum: 100), spacing: 1)
    ]
    
    var body: some View {
        GeometryReader { geo in
            ZStack {
                Rectangle()
                    .fill(
                        customIcon.bgColor
                    )
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                
                LazyVGrid(columns: columns, alignment: .leading, spacing: 0) {
                    ForEach(customIcon.background, id: \.self.1) { (bgOption, uuid) in
                        bgOption.image
                            .resizable()
                            .aspectRatio(1, contentMode: .fill)
                            .foregroundColor(customIcon.bgOverlayColor)
                    }
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(
                    .clear
                )
                
                Circle()
                    .strokeBorder(customIcon.circleStrokeColor, lineWidth: geo.size.width * 0.045)
                    .background(Circle().fill(customIcon.innerColor))
                    .frame(width: geo.size.width*facePercSize,
                           height: geo.size.height*facePercSize,
                           alignment: .center)
                    .position(x: geo.size.width/2, y: geo.size.height/2)
                
                customIcon.leftEye.image
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: geo.size.width*0.12,
                           height: geo.size.height*0.12,
                           alignment: .center)
                    .position(x: geo.size.width*0.4,
                              y: geo.size.height*0.4)
                    .foregroundColor(customIcon.leftEyeColor)
                
                customIcon.rightEye.image
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: geo.size.width*0.12,
                           height: geo.size.height*0.12,
                           alignment: .center)
                    .position(x: geo.size.width*0.6,
                              y: geo.size.height*0.4)
                    .foregroundColor(customIcon.rightEyeColor)
                
                customIcon.mouth.image
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: geo.size.width*0.12,
                           height: geo.size.height*0.12,
                           alignment: .center)
                    .position(x: geo.size.width*0.5,
                              y: geo.size.height*0.59)
                    .foregroundColor(customIcon.mouthColor)
            }
            .position(x: geo.size.width/2,
                      y: geo.size.height/2 - entireFuckingViewOffset)
        }
    }
}

struct IconView_Previews: PreviewProvider {
    static var backgrounds: [(BackGroundOptions, UUID)] = {
        var blah = [(BackGroundOptions, UUID)]()
        for _ in 0...CustomIcon.numberOfBGItems {
            blah.append((BackGroundOptions.selectable.randomElement()!, UUID()))
        }
        return blah
    }()
    
    static var previews: some View {
        IconView(customIcon: CustomIcon.defaultCustomIcon,
                 isPreview: true)
            .frame(width: 256, height: 256, alignment: .center)
        
    }
}
