//
//  CanCouponView.swift
//  SodaLive
//
//  Created by klaus on 2024/01/03.
//

import SwiftUI

struct CanCouponView: View {
    
    @StateObject var viewModel = CanCouponViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            GeometryReader { proxy in
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "쿠폰등록")
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        VStack(spacing: 0) {
                            Text("쿠폰번호 입력")
                                .font(.custom(Font.bold.rawValue, size: 16.7))
                                .foregroundColor(Color(hex: "eeeeee"))
                                .frame(maxWidth: .infinity, alignment: .leading)
                            
                            TextField("쿠폰번호를 입력하세요", text: $viewModel.couponNumber)
                                .autocapitalization(.allCharacters) // Force uppercase keyboard
                                .textContentType(.none)
                                .disableAutocorrection(true)
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color(hex: "eeeeee"))
                                .padding(.vertical, 16.7)
                                .padding(.horizontal, 13.3)
                                .background(Color(hex: "222222"))
                                .cornerRadius(6.7)
                                .keyboardType(.default)
                                .padding(.top, 13.3)
                                .onChange(of: viewModel.couponNumber) { newString in
                                    let filtered = newString.filter { $0.isUppercase || $0.isNumber }
                                    if filtered != newString {
                                        viewModel.couponNumber = filtered
                                    }
                                }
                            
                            Text("등록하기")
                                .font(.custom(Font.bold.rawValue, size: 18.3))
                                .foregroundColor(.white)
                                .padding(.vertical, 16)
                                .frame(maxWidth: .infinity)
                                .background(Color(hex: "3bb9f1"))
                                .cornerRadius(10)
                                .padding(.top, 21.3)
                                .onTapGesture {
                                    viewModel.useCoupon()
                                }
                            
                            VStack(alignment: .leading, spacing: 0) {
                                Text("등록안내")
                                    .font(.custom(Font.bold.rawValue, size: 13.3))
                                    .foregroundColor(Color(hex: "eeeeee"))
                                
                                CanCouponNoticeItemView(notice: "공백없이 쿠폰번호 16자리를 입력해주세요.")
                                    .padding(.top, 8)
                                
                                CanCouponNoticeItemView(notice: "입력한 쿠폰은 소멸시까지 취소처리가 되지 않습니다.")
                                    .padding(.top, 4)
                                
                                CanCouponNoticeItemView(notice: "충전된 캔은 캔 충전현황에서 확인할 수 있습니다.")
                                    .padding(.top, 4)
                                
                                Rectangle()
                                  .foregroundColor(.clear)
                                  .frame(maxWidth: .infinity, minHeight: 1, maxHeight: 1)
                                  .background(Color(hex: "555555"))
                                  .padding(.vertical, 26.7)
                                
                                Text("주의사항")
                                    .font(.custom(Font.bold.rawValue, size: 13.3))
                                    .foregroundColor(Color(hex: "eeeeee"))
                                
                                CanCouponNoticeItemView(notice: "이벤트 쿠폰을 통해 충전한 캔은 환불되지 않습니다.")
                                    .padding(.top, 8)
                                
                                CanCouponNoticeItemView(notice: "쿠폰은 상업적 용도로 사용하거나 매매할 수 없습니다.")
                                    .padding(.top, 4)
                                
                                CanCouponNoticeItemView(notice: "한번 등록한 쿠폰은 재사용이 불가합니다.")
                                    .padding(.top, 4)
                                
                                CanCouponNoticeItemView(notice: "연령 제한 정책에 따라 쿠폰이용은 본인인증한 회원만 이용 가능합니다.")
                                    .padding(.top, 4)
                            }
                            .padding(.horizontal, 13.3)
                            .padding(.vertical, 26.7)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .background(Color(hex: "13181b"))
                            .cornerRadius(6.7)
                            .padding(.top, 21.3)
                        }
                        .padding(13.3)
                    }
                }
                .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: "9970ff"))
                                .foregroundColor(Color.white)
                                .multilineTextAlignment(.leading)
                                .fixedSize(horizontal: false, vertical: true)
                                .cornerRadius(20)
                                .padding(.top, 66.7)
                            Spacer()
                        }
                    }
                }
            }
        }
    }
}

struct CanCouponView_Previews: PreviewProvider {
    static var previews: some View {
        CanCouponView()
    }
}
