//
//  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.grayee)
                            
                            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.gray90)
                        
                        HStack(spacing: 0) {
                            Text("이벤트")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color.grayee)
                            
                            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.gray90)
                        
                        HStack(spacing: 0) {
                            Text("알림 설정")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color.grayee)
                            
                            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.gray22)
                    .cornerRadius(6.7)
                    .padding(.top, 26.7)
                    
                    if UserDefaults.bool(forKey: .auth) {
                        HStack(spacing: 0) {
                            Text("콘텐츠 보기 설정")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color.grayee)
                            
                            Spacer()
                            
                            Image("ic_forward")
                                .resizable()
                                .frame(width: 20, height: 20)
                        }
                        .padding(.horizontal, 16.7)
                        .frame(width: cardWidth, height: 50)
                        .background(Color.gray22)
                        .cornerRadius(6.7)
                        .contentShape(Rectangle())
                        .padding(.top, 13.3)
                        .onTapGesture {
                            AppState.shared.setAppStep(step: .contentViewSettings)
                        }
                    }
                    
                    VStack(spacing: 0) {
                        HStack(spacing: 0) {
                            Text("이용약관")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color.grayee)
                            
                            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.gray90)
                        
                        HStack(spacing: 0) {
                            Text("개인정보처리방침")
                                .font(.custom(Font.bold.rawValue, size: 14.7))
                                .foregroundColor(Color.grayee)
                            
                            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.gray22)
                    .cornerRadius(6.7)
                    .padding(.top, 13.3)
                    
                    HStack(spacing: 0) {
                        Text("앱 버전 정보")
                            .font(.custom(Font.bold.rawValue, size: 14.7))
                            .foregroundColor(Color.grayee)
                        
                        Spacer()
                        
                        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
                        
                        Text("Ver \(version!)")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color.grayee)
                    }
                    .padding(.horizontal, 16.7)
                    .frame(width: cardWidth, height: 50)
                    .background(Color.gray22)
                    .cornerRadius(6.7)
                    .padding(.top, 13.3)
                    
                    Text("""
- 회사명 : 주식회사 소다라이브

- 대표자 : 이재형

- 주소 : 경기도 성남시 분당구 황새울로335번길 10, 5층 563A호

- 사업자등록번호 : 870-81-03220

- 통신판매업신고 : 제2024-성남분당B-1012호

- 고객센터 : 02.2055.1477 (이용시간 10:00~19:00)

- 대표 이메일 : sodalive.official@gmail.com
""")
                    .font(.custom(Font.medium.rawValue, size: 11))
                    .foregroundColor(Color.gray77)
                    .frame(width: cardWidth, alignment: .leading)
                    .padding(.top, 13.3)
                    
                    Spacer()
                    
                    Text("로그아웃")
                        .font(.custom(Font.bold.rawValue, size: 14.7))
                        .foregroundColor(Color.grayee)
                        .frame(width: cardWidth, height: 50)
                        .background(Color.gray22)
                        .cornerRadius(6.7)
                        .onTapGesture {
                            isShowLogoutDialog = true
                        }
                    
                    Text("모든 기기에서 로그아웃")
                        .font(.custom(Font.medium.rawValue, size: 14.7))
                        .foregroundColor(Color.gray77)
                        .padding(.top, 13.3)
                        .onTapGesture {
                            isShowLogoutAllDeviceDialog = true
                        }
                    
                    Text("회원탈퇴")
                        .font(.custom(Font.medium.rawValue, size: 14.7))
                        .foregroundColor(Color.gray77)
                        .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.button)
                        .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()
    }
}
