//
//  NicknameUpdateView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/19.
//

import SwiftUI

struct NicknameUpdateView: View {
    
    @StateObject var viewModel = NicknameUpdateViewModel()
    @StateObject var keyboardHandler = KeyboardHandler()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            GeometryReader { proxy in
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "프로필 수정")
                    
                    Text("닉네임 변경으로 인해 피해를 입는 사용자가 지속적으로 발생하여 닉네임 변경을 부득이하게 유료로 전환합니다.")
                        .fixedSize(horizontal: false, vertical: true)
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color(hex: "eeeeee"))
                        .frame(width: screenSize().width - 40, alignment: .leading)
                        .padding(.top, 40)
                    
                    Text("최초 1회에 한해서 무료로 변경이 가능하고, 그 이후부터는 유료로 전환됩니다.")
                        .fixedSize(horizontal: false, vertical: true)
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color(hex: "dd4500"))
                        .frame(width: screenSize().width - 40, alignment: .leading)
                    
                    UserTextField(
                        title: "닉네임(최대 12자)",
                        hint: "닉네임",
                        isSecure: false,
                        variable: $viewModel.nickname
                    )
                    .frame(width: screenSize().width - 40)
                    .padding(.top, 40)
                    
                    Text("중복확인")
                        .font(.custom(Font.bold.rawValue, size: 13.3))
                        .foregroundColor(Color(hex: "eeeeee"))
                        .frame(width: screenSize().width - 40)
                        .padding(.vertical, 13.3)
                        .overlay(
                            RoundedRectangle(cornerRadius: 8)
                                .stroke(Color(hex: "3bb9f1"), lineWidth: 1)
                        )
                        .padding(.top, 21.3)
                        .onTapGesture {
                            hideKeyboard()
                            viewModel.checkNickname()
                        }
                    
                    Spacer()
                    
                    Text(viewModel.price > 0 ? "\(viewModel.price)캔으로 닉네임 변경하기" : "닉네임 변경하기")
                        .font(.custom(Font.bold.rawValue, size: 18.3))
                        .foregroundColor(Color.white)
                        .padding(.vertical, 16)
                        .frame(width: screenSize().width - 26.7)
                        .background(Color(hex: "3bb9f1"))
                        .cornerRadius(10)
                        .padding(.vertical, 13.7)
                        .frame(width: screenSize().width)
                        .background(Color(hex: "222222"))
                        .cornerRadius(16.7)
                        .padding(.top, 13.3)
                        .onTapGesture { viewModel.changeNickname() }
                    
                    if proxy.safeAreaInsets.bottom > 0 {
                        Rectangle()
                            .foregroundColor(Color.black)
                            .frame(width: proxy.size.width, height: 15.3)
                    }
                }
                .edgesIgnoringSafeArea(.bottom)
                .onTapGesture {
                    hideKeyboard()
                }
            }
            .onAppear {
                viewModel.nickname = UserDefaults.string(forKey: .nickname)
                viewModel.getChangeNicknamePrice()
            }
            .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.button)
                            .foregroundColor(Color.white)
                            .multilineTextAlignment(.leading)
                            .fixedSize(horizontal: false, vertical: true)
                            .cornerRadius(20)
                            .padding(.top, 66.7)
                        Spacer()
                    }
                }
            }
        }
    }
}