//
//  SwitchableView.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 1/30/22.
//

import SwiftUI

enum MainSwitchableViewType: Int, CaseIterable {
    case total
    case percentageCircle
    case percentage
    
    func next() -> MainSwitchableViewType {
        let currentValue = self.rawValue
        var next = currentValue + 1
        if next == MainSwitchableViewType.allCases.count {
            next = 0
        }
        return MainSwitchableViewType(rawValue: next) ?? .total
    }
}

struct SwitchableView: View {
    @Binding var viewType: MainSwitchableViewType
    var headerTypeChanged: ((MainSwitchableViewType) -> Void)
    let daysBack: Int

    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
    
    init(daysBack: Int, viewType: Binding<MainSwitchableViewType>, headerTypeChanged: @escaping ((MainSwitchableViewType) -> Void)) {
        self.daysBack = daysBack
        self.headerTypeChanged = headerTypeChanged
        self._viewType = viewType
    }
    
    var body: some View {
        VStack {
            ZStack {
                switch viewType {
                case .total:
                    HeaderStatsView(fakeData: false, backDays: daysBack)
                        .padding([.leading, .trailing], -15)
                        .padding([.top, .bottom], 8)
                        .allowsHitTesting(false)
                case .percentageCircle:
                    HeaderPercView(fakeData: false, backDays: daysBack, type: .circular)
                        .allowsHitTesting(false)
                case .percentage:
                    HeaderPercView(fakeData: false, backDays: daysBack, type: .text)
                        .allowsHitTesting(false)
                }

                VStack {
                    HStack {
                        Spacer()
                        Image(systemName: "arrow.triangle.2.circlepath.circle")
                            .resizable()
                            .frame(width: 20, height: 20, alignment: .trailing)
                    }
                    Spacer()
                }
                .padding(.trailing, 8)
                .padding(.top, 12)
                .allowsHitTesting(false)
                .foregroundColor(Color(UIColor.systemGray))
            }
            .padding(.top, -7)

            Text(String(format: String(localized: "content_view_header_title"), daysBack))
                .font(.body)
                .foregroundColor(Color(UIColor.systemGray))
                .frame(maxWidth: .infinity, alignment: .center)
                .padding(.top, -12)
        }
        .background(
            Color(theme.currentTheme.secondaryBGColor)
        )
        .contentShape(Rectangle())
        .cornerRadius(10, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
        .padding(.bottom, 30)
        .onTapGesture {
            viewType = viewType.next()
            self.headerTypeChanged(viewType)
        }
    }
}

struct SwitchableView_Previews: PreviewProvider {
    static var previews: some View {
        SwitchableView(daysBack: 30, viewType: .constant(.total), headerTypeChanged: { _ in
        })
    }
}
