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

import SwiftUI

struct SettingsView: View {
    @State private var isShowLogoutDialog = false
    @State private var isShowLogoutAllDeviceDialog = false
    
    @StateObject var viewModel = SettingsViewModel()
    
    var body: some View {
        let cardWidth = screenSize().width - 26.7
        
        BaseView(isLoading: $viewModel.isLoading) {
            GeometryReader { geo in
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "설정")
                    VStack(spacing: 0) {
                        HStack(spacing: 0) {
                            Text("공지사항")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color(hex: "eeeeee"))
                            
                            Spacer()
                            
                            Image("ic_forward")
                                .resizable()
                                .frame(width: 20, height: 20)
                        }
                        .padding(.horizontal, 3.3)
                        .frame(width: cardWidth - 26.7, height: 50)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            AppState.shared.setAppStep(step: .notices)
                        }
                        
                        Rectangle()
                            .frame(width: cardWidth - 26.7, height: 0.3)
                            .foregroundColor(Color(hex: "909090"))
                        
                        HStack(spacing: 0) {
                            Text("이벤트")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color(hex: "eeeeee"))
                            
                            Spacer()
                            
                            Image("ic_forward")
                                .resizable()
                                .frame(width: 20, height: 20)
                        }
                        .padding(.horizontal, 3.3)
                        .frame(width: cardWidth - 26.7, height: 50)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            AppState.shared.setAppStep(step: .events)
                        }
                        
                        Rectangle()
                            .frame(width: cardWidth - 26.7, height: 0.3)
                            .foregroundColor(Color(hex: "909090"))
                        
                        HStack(spacing: 0) {
                            Text("알림 설정")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color(hex: "eeeeee"))
                            
                            Spacer()
                            
                            Image("ic_forward")
                                .resizable()
                                .frame(width: 20, height: 20)
                        }
                        .padding(.horizontal, 3.3)
                        .frame(width: cardWidth - 26.7, height: 50)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            AppState.shared.setAppStep(step: .notificationSettings)
                        }
                    }
                    .padding(.horizontal, 13.3)
                    .frame(width: cardWidth)
                    .background(Color(hex: "222222"))
                    .cornerRadius(6.7)
                    .padding(.top, 26.7)
                    
                    VStack(spacing: 0) {
                        HStack(spacing: 0) {
                            Text("이용약관")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color(hex: "eeeeee"))
                            
                            Spacer()
                            
                            Image("ic_forward")
                                .resizable()
                                .frame(width: 20, height: 20)
                        }
                        .padding(.horizontal, 3.3)
                        .frame(width: cardWidth - 26.7, height: 50)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            AppState.shared.setAppStep(step: .terms)
                        }
                        
                        Rectangle()
                            .frame(width: cardWidth - 26.7, height: 0.3)
                            .foregroundColor(Color(hex: "909090"))
                        
                        HStack(spacing: 0) {
                            Text("개인정보처리방침")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color(hex: "eeeeee"))
                            
                            Spacer()
                            
                            Image("ic_forward")
                                .resizable()
                                .frame(width: 20, height: 20)
                        }
                        .padding(.horizontal, 3.3)
                        .frame(width: cardWidth - 26.7, height: 50)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            AppState.shared.setAppStep(step: .privacy)
                        }
                    }
                    .padding(.horizontal, 13.3)
                    .frame(width: cardWidth)
                    .background(Color(hex: "222222"))
                    .cornerRadius(6.7)
                    .padding(.top, 13.3)
                    
                    HStack(spacing: 0) {
                        Text("앱 버전 정보")
                            .font(.custom(Font.bold.rawValue, size: 14.7))
                            .foregroundColor(Color(hex: "eeeeee"))
                        
                        Spacer()
                        
                        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
                        
                        Text("Ver \(version!)")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "eeeeee"))
                    }
                    .padding(.horizontal, 16.7)
                    .frame(width: cardWidth, height: 50)
                    .background(Color(hex: "222222"))
                    .cornerRadius(6.7)
                    .padding(.top, 13.3)
                    
                    Spacer()
                    
                    Text("로그아웃")
                        .font(.custom(Font.bold.rawValue, size: 14.7))
                        .foregroundColor(Color(hex: "eeeeee"))
                        .frame(width: cardWidth, height: 50)
                        .background(Color(hex: "222222"))
                        .cornerRadius(6.7)
                        .onTapGesture {
                            isShowLogoutDialog = true
                        }
                    
                    Text("모든 기기에서 로그아웃")
                        .font(.custom(Font.medium.rawValue, size: 14.7))
                        .foregroundColor(Color(hex: "777777"))
                        .padding(.top, 13.3)
                        .onTapGesture {
                            isShowLogoutAllDeviceDialog = true
                        }
                    
                    Text("회원탈퇴")
                        .font(.custom(Font.medium.rawValue, size: 14.7))
                        .foregroundColor(Color(hex: "777777"))
                        .underline()
                        .padding(.vertical, 26.7)
                        .onTapGesture {
                            AppState.shared.setAppStep(step: .signOut)
                        }
                }
            }
            
            if isShowLogoutDialog {
                SodaDialog(
                    title: "알림",
                    desc: "로그아웃 하시겠어요?",
                    confirmButtonTitle: "확인",
                    confirmButtonAction: {
                        viewModel.logout {
                            self.isShowLogoutDialog = false
                            AppState.shared.setAppStep(step: .main)
                            UserDefaults.reset()
                        }
                    },
                    cancelButtonTitle: "취소",
                    cancelButtonAction: {
                        self.isShowLogoutDialog = false
                    }
                )
            }
            
            if isShowLogoutAllDeviceDialog {
                SodaDialog(
                    title: "알림",
                    desc: "모든 기기에서 로그아웃 하시겠어요?",
                    confirmButtonTitle: "확인",
                    confirmButtonAction: {
                        viewModel.logoutAllDevice {
                            self.isShowLogoutDialog = false
                            AppState.shared.setAppStep(step: .main)
                            UserDefaults.reset()
                        }
                    },
                    cancelButtonTitle: "취소",
                    cancelButtonAction: {
                        self.isShowLogoutDialog = false
                    }
                )
            }
        }
        .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 SettingsView_Previews: PreviewProvider {
    static var previews: some View {
        SettingsView()
    }
}