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

import SwiftUI

struct SignOutView: View {
    
    @StateObject var viewModel = SignOutViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "회원탈퇴")
                
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(spacing: 0) {
                        VStack(spacing: 13.3) {
                            Text("정말로 탈퇴하실 거에요?\n한 번 더 생각해보지 않으실래요?")
                                .font(.custom(Font.bold.rawValue, size: 20))
                                .foregroundColor(Color(hex: "3bb9f1"))
                                .frame(width: screenSize().width - 26.7, alignment: .leading)
                            
                            Text("계정을 삭제하려는 이유를 선택해주세요.\n서비스 개선에 중요한 자료로 활용하겠습니다.")
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color(hex: "eeeeee"))
                                .frame(width: screenSize().width - 26.7, alignment: .leading)
                            
                            VStack(alignment: .leading, spacing: 16.7) {
                                ForEach(0..<viewModel.reasons.count, id: \.self) { index in
                                    let reason = viewModel.reasons[index]
                                    
                                    HStack(spacing: 13.3) {
                                        Image(
                                            viewModel.reasonSelectedIndex == index ?
                                            "btn_radio_select_selected" :
                                                "btn_radio_select_normal"
                                        )
                                        .resizable()
                                        .frame(width: 20, height: 20)
                                        
                                        Text(reason)
                                            .font(.custom(Font.medium.rawValue, size: 13.3))
                                            .foregroundColor(Color(hex: "eeeeee"))
                                        
                                        if index == viewModel.reasons.count - 1 {
                                            VStack(spacing: 6.7) {
                                                TextField("입력해주세요", text: $viewModel.reason)
                                                    .autocapitalization(.none)
                                                    .disableAutocorrection(true)
                                                    .font(.custom(Font.medium.rawValue, size: 13.3))
                                                    .foregroundColor(Color(hex: "eeeeee"))
                                                    .keyboardType(.webSearch)
                                                
                                                Rectangle()
                                                    .frame(height: 1)
                                                    .foregroundColor(Color(hex: "909090").opacity(0.5))
                                            }
                                        }
                                    }
                                    .onTapGesture {
                                        viewModel.reasonSelectedIndex = index
                                    }
                                }
                            }
                            .padding(.top, 13.3)
                            .padding(.horizontal, 16.7)
                            
                            Rectangle()
                                .frame(width: screenSize().width, height: 6.7)
                                .foregroundColor(Color(hex: "232323"))
                                .padding(.top, 20)
                            
                            Text("계정을 삭제하면 회원님의 모든 콘텐츠와 활동 길고, 캔충전 및 적립, 사용내역 등의 기록이 삭제됩니다. 삭제된 정보는 복구할 수 없으니 신중히 결정해주세요.\n캔 충전하기를 통해 적립한 캔은 계정 삭제시 환불이 불가합니다. 또한 환불 신청 후 환불처리가 되기 전에 계정을 삭제하는 경우 포인트 사용내역을 확인할 수 없어 환불이 불가합니다.")
                                .font(.custom(Font.medium.rawValue, size: 12))
                                .foregroundColor(Color(hex: "ff5c49"))
                                .fixedSize(horizontal: false, vertical: true)
                                .padding(.horizontal, 26.7)
                                .padding(.top, 16.7)
                            
                            UserTextField(
                                title: "비밀번호 확인",
                                hint: "비밀번호를 입력해주세요.",
                                isSecure: true,
                                variable: $viewModel.password,
                                keyboardType: .emailAddress
                            )
                            .padding(.top, 20)
                            .padding(.horizontal, 26.7)
                            
                            Text("탈퇴하기")
                                .font(.custom(Font.bold.rawValue, size: 15))
                                .foregroundColor(.white)
                                .padding(.vertical, 16)
                                .frame(width: screenSize().width - 26.7)
                                .background(Color(hex: "3bb9f1"))
                                .cornerRadius(6.7)
                                .padding(.top, 26.7)
                                .onTapGesture {
                                    viewModel.signOut()
                                }
                        }
                        .padding(.top, 26.7)
                    }
                }
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .padding(.horizontal, 6.7)
                        .frame(width: geo.size.width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color(hex: "3bb9f1"))
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .fixedSize(horizontal: false, vertical: true)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
    }
}

struct SignOutView_Previews: PreviewProvider {
    static var previews: some View {
        SignOutView()
    }
}