//
//  ContentView.swift
//  JJUNGTABLE
//
//  Created by Sean Kim on 3/5/24.
//

import SwiftUI
import Combine


struct ContentView: View {
    @EnvironmentObject var viewModel: ViewModel
    
    @StateObject private var login = Login()
    
    @State private var cancellables = Set<AnyCancellable>()
    
    
    @State private var selectedOption = "Option 1"
    let options = ["Option 1", "Option 2", "Option 3", "Option 4"]
    @State private var phoneNum = ("0000", "0000")
    
    var body: some View {
        VStack(spacing: 0) {
            Spacer()
            Button {
                Login().tryKakaoLogin()
                    .sink { completion in
                        if case .failure(_) = completion {
                            viewModel.alertData = .init(body: "SNS 로그인에 실패했습니다.")
                            viewModel.showAlert.toggle()
                        }
                    } receiveValue: { data in
                        printLog(data)
                        API.shared.readData(path: "/db/userInfo/read",
                        queryItems: [
                                        URLQueryItem(name: "id", value: data),
                                        URLQueryItem(name: "type", value: "01")
                                    ])
                        .sink { completion in
                            switch completion {
                            case .finished:
                                break
                            case .failure(let error):
                                printLog(error)
                            }
                        } receiveValue: { data in
                            do {
                                let decoder = JSONDecoder()
                                let cid = try decoder.decode([UserCID].self, from: data)
                                printLog(cid)
                            } catch {
                                printLog(error)
                                self.viewModel.alertData = self.viewModel.systemErrorAlert()
                                self.viewModel.showAlert.toggle()
                            }
                        }
                        .store(in: &cancellables)
                    }
                    .store(in: &cancellables)
                
            } label: {
                Text("Kakao ID로 로그인")
                    .font(.nps(font: .bold, size: 32))
            }
            .padding(.bottom,20)
            
            Button {
                login.tryAppleLogin()
                    .sink { completion in
                        
                    } receiveValue: { data in
                        printLog("APPLE ID: \(data)")
                    }
                    .store(in: &cancellables)

            } label: {
                Text("Apple ID로 로그인")
                    .font(.nps(font: .bold, size: 32))
            }
            
        
            HStack(spacing: 0){
                Picker("Select an option", selection: $selectedOption) {
                    ForEach(options, id: \.self) { option in
                        Text(option).tag(option)
                    }
                }
                .pickerStyle(MenuPickerStyle()) // This line makes the Picker a dropdown menu
                .background {
                    RoundedRectangle(cornerRadius: 10)
                        .foregroundStyle(.clear)
                        .frame(height: 50)
                }
                
                TextField("digit 4",text: $phoneNum.0)
                
                TextField("last",text: $phoneNum.1)
                    .font(.Content)
                    .padding([.top,.bottom,.leading],5)
                    .frame(height: 50)
                    .cornerRadius(10)

            }
            .background {
                RoundedRectangle(cornerRadius: 10)
                    .stroke(Color.brand,lineWidth: 2)
                    .foregroundStyle(.clear)
            }
            .padding()

            
            Button {
                login.sendCodeToPhone()
            } label: {
                    Text("code")
                        .font(.nps(font: .bold, size: 32))
            }
            
            Button {
                login.verifyCode(code: phoneNum.1)
            } label: {
                    Text("veri")
                        .font(.nps(font: .bold, size: 32))
            }
            
            Spacer()
            
        }
        .fullPage(.pageBack)
        .setBaseViewModifier()
    }
}
