//
//  InputUserNameView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/13.
//

import SwiftUI

struct InputUserNameView: View {
    @ObservedObject var sessionLogin: SessionLogin = SessionLogin()
    @Binding var isLogin: Bool
    @Binding var param: LoginViewParam
    @Binding var viewMode : LoginViewMode
    @State private var isEmptyAlert = false

    var body: some View {
        VStack{
            VStack(alignment: .leading, spacing: 0) {
                Text(SharingData.my.shipName)
                    .font(FontStyle.TitleSBold.font)
                    .padding(.leading, 10)
                    .foregroundColor(.white)

                HStack(spacing: 0){
                    Image("account")
                        .resizable()
                        .frame(width: 22, height: 22)
                        .padding(EdgeInsets(top: 14, leading: 8, bottom: 13, trailing: 18))

                    TextField(
                        "",
                        text: $param.userName,
                        prompt: Text("Please set user name.")
                            .font(FontStyle.SupplementText.font)
                            .foregroundColor(ColorSet.BodyDescriptiion.color)
                    )
                    .frame(height: 49)
                    .padding(.zero)
                }

                Divider()
                    .frame(height: 1)
                    .background(ColorSet.LineColor03.color)
            }
            .frame(height: 88)

            Button(action: {
                if param.userName.isEmpty {
                    isEmptyAlert = true
                } else {
                    Preferences.UserName = param.userName
                    isLogin = true
                }
            }, label: {
                Text("Sign In")
                    .font(FontStyle.TitleS.font)
                    .frame(height: 60)
                    .frame(maxWidth: .infinity)
                    .foregroundColor(ColorSet.ButtonText.color)
                    .background(ColorSet.LoginBk01.color)
            })
            .alert(isPresented: $isEmptyAlert) {
                return Alert(title: Text("error"),
                             message: Text("User Nmae not entered.")
                )
            }
            .padding(.top, 40)
        }
        .padding(EdgeInsets(top: 21, leading: 20, bottom: 32, trailing: 20))
        .background(ColorSet.LoginBk02.color)
        .padding(.horizontal, 30)
    }
}

#Preview {
    InputUserNameView(isLogin: .constant(false), param: .constant(LoginViewParam()), viewMode: .constant(.InputIdPassword))
}
