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

import SwiftUI

enum LoginViewMode{
    ///QRを読み込むor ID/PW入力か選択する画面
    case SelectType
    /// ID/PW入力
    case InputIdPassword
    /// ユーザー名入力
    case InputUserName
}

class LoginViewParam: ObservableObject{
    @Published var viewMode: LoginViewMode = .SelectType
    @Published var shipId: String = ""
    @Published var password: String = ""
    @Published var userName: String = ""
}

struct LoginView: View {
    @State var isQrRead: Bool = false
    @Binding var isLogin: Bool
    @ObservedObject var scannerViewModel = ScannerViewModel()
    @EnvironmentObject var loginViewParam: LoginViewParam
    
    var body: some View {
        ZStack{
            Triangle()
                .fill(Color.red)
                .ignoresSafeArea()
            VStack{
                Spacer()
                    .frame(height: 64)
                //ロゴ
                Image("logo_signin")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 135, height: 66.85)
                
                Spacer()
                    .frame(height: 20)
                
                Image("h1_signin")
                    .frame(width: 250, height: 21.94)
                
                Spacer()
                //                .frame(height: 60)
                
                switch loginViewParam.viewMode{
                case .SelectType:
                    LoginTypeSelectView(isQrRead: $isQrRead)
                case .InputIdPassword:
                    InputIdPassWordView()
                case .InputUserName:
                    InputUserNameView(isLogin: $isLogin)
                    
                }
                
                Spacer()
                
                VStack(spacing: 40){
                    VStack(spacing: 30) {
                        // TODO: サイトのアドレスを変更
                        Link("Site policy", destination: URL(string: "https://www.google.co.jp/")!)
                        Link("Cookie policy", destination: URL(string: "https://www.google.co.jp/")!)
                        Link("Privacy policy", destination: URL(string: "https://www.google.co.jp/")!)
                    }
                    .font(FontStyle.SupplementText.font)
                    .foregroundColor(ColorSet.Body.color)
                    
                    Text("Ver. \(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String)")
                        .font(FontStyle.VersionText.font)
                        .foregroundColor(ColorSet.BodyDescriptiion.color)
                }
                .padding(.bottom, 30)
            }
        }
        .fullScreenCover(isPresented: $isQrRead, content: {
            QRReadView(viewMode: .constant(.SelectType))
        })
    }
}

fileprivate struct Triangle: Shape{
    func path(in rect: CGRect) -> Path {
        Path{ path in
            let screen = UIScreen.main.bounds
            path.move(to: CGPoint(x: 63, y: screen.maxY))
            path.addLine(to: CGPoint(x: screen.maxX, y: 165))
            path.addLine(to: CGPoint(x: screen.maxX, y: screen.maxY))
            path.closeSubpath()
        }
    }
}

#Preview {
    LoginView(isLogin: .constant(false))
}
