//
//  AuditionApplyView.swift
//  SodaLive
//
//  Created by klaus on 1/7/25.
//

import SwiftUI

struct AuditionApplyView: View {
    
    @Binding var isShowing: Bool
    @Binding var phoneNumber: String
    let filename: String
    let onClickApply: () -> Void
    
    @State private var isShowPopup = false
    @State private var errorMessage = ""
    
    @State private var isShow: Bool = false
    @State private var isAgree = false
    
    var body: some View {
        ZStack {
            Color.black.opacity(0.7).ignoresSafeArea()
                .onTapGesture {
                    isShowing = false
                }
            
            VStack(spacing: 0) {
                Spacer()
                
                if isShow {
                    VStack(alignment: .leading, spacing: 0) {
                        HStack(spacing: 0) {
                            Text("오디션 지원")
                                .font(.custom(Font.medium.rawValue, size: 18.3))
                                .foregroundColor(.white)
                            
                            Spacer()
                            
                            Image("ic_noti_stop")
                                .onTapGesture {
                                    isShowing = false
                                }
                        }
                        
                        Text("녹음파일")
                            .font(.custom(Font.bold.rawValue, size: 16.7))
                            .foregroundColor(.grayee)
                            .padding(.top, 20)
                        
                        HStack(spacing: 4) {
                            Image("ic_note_square")
                            
                            Text(filename)
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(.grayd2)
                            
                            Spacer()
                        }
                        .padding(.horizontal, 13.3)
                        .padding(.vertical, 8)
                        .frame(maxWidth: .infinity)
                        .background(Color.bg)
                        .cornerRadius(5.3)
                        .padding(.top, 10)
                        
                        Text("연락처")
                            .font(.custom(Font.bold.rawValue, size: 16.7))
                            .foregroundColor(.grayee)
                            .padding(.top, 15)
                        
                        TextField("합격시 받을 연락처를 남겨주세요", text: $phoneNumber)
                            .autocapitalization(.none)
                            .disableAutocorrection(true)
                            .keyboardType(.decimalPad)
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(.grayee)
                            .padding(.horizontal, 13.3)
                            .padding(.vertical, 17)
                            .frame(maxWidth: .infinity)
                            .background(Color.bg)
                            .cornerRadius(5.3)
                            .padding(.top, 10)
                        
                        HStack(alignment: .top, spacing: 13.3) {
                            Image(isAgree ? "btn_select_checked" : "btn_select_normal")
                                .resizable()
                                .frame(width: 20, height: 20)
                            
                            Text("보이스온 오디오 드라마 오디션 합격시 개인 연락을 위한 개인 정보(연락처) 수집 및 활용에 동의합니다.\n오디션 지원자는 개인정보 수집 및 활용 동의에 거부할 권리가 있으며 비동의시 오디션 지원은 취소 됩니다.")
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color.grayee)
                                .lineSpacing(3)
                        }
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(.top, 30)
                        .onTapGesture {
                            isAgree.toggle()
                        }
                        
                        Text("오디션 지원하기")
                            .font(.custom(Font.bold.rawValue, size: 13.3))
                            .foregroundColor(Color.grayee)
                            .padding(.vertical, 13.3)
                            .frame(maxWidth: .infinity)
                            .background(Color.button)
                            .cornerRadius(8)
                            .padding(.top, 35)
                            .onTapGesture {
                                if !isAgree {
                                    errorMessage = "연락처 수집 및 활용에 동의하셔야 오디션 지원이 가능합니다."
                                    isShowPopup = true
                                    return
                                }
                                
                                onClickApply()
                            }
                    }
                    .frame(maxWidth:.infinity)
                    .padding(.horizontal, 16)
                    .padding(.top, 16)
                    .padding(.bottom, 32)
                    .background(Color.gray22)
                    .cornerRadius(10, corners: [.topLeft, .topRight])
                    .transition(.move(edge: .bottom))
                    .animation(.easeInOut(duration: 0.5), value: isShow)
                }
            }
            .ignoresSafeArea()
        }
        .popup(isPresented: $isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(errorMessage)
                        .padding(.vertical, 13.3)
                        .frame(width: screenSize().width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.center)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
        .onAppear {
            withAnimation {
                isShow = true
            }
        }
    }
}

#Preview {
    AuditionApplyView(
        isShowing: .constant(true),
        phoneNumber: .constant(""),
        filename: "voiceon_now_voice_9292939.m4a",
        onClickApply: {}
    )
}