//
//  LiveRoomPasswordDialog.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import SwiftUI

struct LiveRoomPasswordDialog: View {
    
    @Binding var isShowing: Bool
    
    let can: Int
    let confirmAction: (String) -> Void
    
    @State private var password = ""
    @StateObject var keyboardHandler = KeyboardHandler()
    
    var body: some View {
        GeometryReader { geo in
            ZStack {
                Color.black
                    .opacity(0.5)
                    .frame(width: geo.size.width, height: geo.size.height)
                
                VStack(spacing: 0) {
                    Text("비밀번호 입력")
                        .font(.custom(Font.bold.rawValue, size: 18.3))
                        .foregroundColor(Color(hex: "bbbbbb"))
                        .padding(.top, 40)
                    
                    Text("비공개 라이브의 입장 비밀번호를\n입력해 주세요.")
                        .font(.custom(Font.medium.rawValue, size: 13))
                        .foregroundColor(Color(hex: "bbbbbb"))
                        .multilineTextAlignment(.center)
                        .padding(.top, 12)
                        .padding(.horizontal, 13.3)
                    
                    UserTextField(
                        title: "비밀번호",
                        hint: "비밀번호를 입력해 주세요",
                        isSecure: false,
                        variable: $password,
                        keyboardType: .numberPad
                    )
                    .padding(.horizontal, 13.3)
                    .padding(.top, 13.3)
                    
                    HStack(spacing: 13.3) {
                        Text("취소")
                            .font(.custom(Font.bold.rawValue, size: 15.3))
                            .foregroundColor(Color(hex: "3bb9f1"))
                            .padding(.vertical, 16)
                            .frame(width: (geo.size.width - 66.7) / 3)
                            .background(Color(hex: "13181b"))
                            .cornerRadius(8)
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .stroke(Color(hex: "3bb9f1"), lineWidth: 1)
                            )
                            .onTapGesture {
                                isShowing = false
                            }
                        
                        if can > 0 {
                            HStack(spacing: 0) {
                                Text("\(can)")
                                    .font(.custom(Font.bold.rawValue, size: 15.3))
                                    .foregroundColor(Color(hex: "ffffff"))
                                
                                Image("ic_can")
                                    .resizable()
                                    .frame(width: 20, height: 20)
                                    
                                Text("으로 입장")
                                    .font(.custom(Font.bold.rawValue, size: 15.3))
                                    .foregroundColor(Color(hex: "ffffff"))
                            }
                            .padding(.vertical, 16)
                            .frame(width: (geo.size.width - 66.7) * 2 / 3)
                            .background(Color(hex: "3bb9f1"))
                            .cornerRadius(8)
                            .onTapGesture {
                                if password.trimmingCharacters(in: .whitespaces).isEmpty {
                                    confirmAction("")
                                } else {
                                    confirmAction(password)
                                }
                                isShowing = false
                            }
                        } else {
                            Text("입장하기")
                                .font(.custom(Font.bold.rawValue, size: 15.3))
                                .foregroundColor(Color(hex: "ffffff"))
                                .padding(.vertical, 16)
                                .frame(width: (geo.size.width - 66.7) * 2 / 3)
                                .background(Color(hex: "3bb9f1"))
                                .cornerRadius(8)
                                .onTapGesture {
                                    if password.trimmingCharacters(in: .whitespaces).isEmpty {
                                        confirmAction("")
                                    } else {
                                        confirmAction(password)
                                    }
                                    isShowing = false
                                }
                        }
                    }
                    .padding(.top, 45)
                    .padding(.bottom, 16.7)
                }
                .frame(width: geo.size.width - 26.7, alignment: .center)
                .background(Color(hex: "222222"))
                .cornerRadius(10)
                .offset(y: 0 - (keyboardHandler.keyboardHeight / 3))
            }
        }
    }
}

struct LiveRoomPasswordDialog_Previews: PreviewProvider {
    static var previews: some View {
        LiveRoomPasswordDialog(
            isShowing: .constant(true),
            can: 10,
            confirmAction: { _ in }
        )
    }
}