//
//  MessageView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import SwiftUI

struct MessageView: View {
    
    @StateObject var viewModel = MessageViewModel()
    
    var body: some View {
        GeometryReader { geo in
            VStack {
                HomeNavigationBar(title: "메시지") {}
                
                Tab()
                
                Text("※ 보관하지 않은 받은 메시지는 3일 후, 자동 삭제됩니다.")
                    .font(.custom(Font.medium.rawValue, size: 13.3))
                    .padding(.top, 20)
                
                switch viewModel.currentTab {
                case .text:
                    TextMessageView()
                    
                case .voice:
                    VoiceMessageView()
                }
            }
            .frame(width: geo.size.width, height: geo.size.height)
        }
    }
    
    @ViewBuilder
    func Tab() -> some View {
        let tabWidth = screenSize().width / 2
        
        VStack(spacing:0) {
            HStack(spacing: 0) {
                Button(action: {
                    if viewModel.currentTab != .text {
                        viewModel.currentTab = .text
                    }
                }) {
                    VStack(spacing: 0) {
                        Text("문자")
                            .font(.custom(Font.medium.rawValue, size: 16.7))
                            .foregroundColor(Color(hex: viewModel.currentTab == .text ? "eeeeee" : "777777"))
                            .frame(width: tabWidth, height: 50)
                        
                        if viewModel.currentTab == .text {
                            Rectangle()
                                .foregroundColor(Color(hex: "3bb9f1"))
                                .frame(width: tabWidth, height: 3)
                        }
                    }
                }
                
                Button(action: {
                    if viewModel.currentTab != .voice {
                        viewModel.currentTab = .voice
                    }
                }) {
                    VStack(spacing: 0) {
                        Text("음성")
                            .font(.custom(Font.medium.rawValue, size: 16.7))
                            .foregroundColor(Color(hex: viewModel.currentTab == .voice ? "eeeeee" : "777777"))
                            .frame(width: tabWidth, height: 50)
                        
                        if viewModel.currentTab == .voice {
                            Rectangle()
                                .foregroundColor(Color(hex: "3bb9f1"))
                                .frame(width: tabWidth, height: 3)
                        }
                    }
                }
            }
            
            Rectangle()
                .frame(width: screenSize().width, height: 1)
                .foregroundColor(Color(hex: "909090"))
        }
    }
}

struct MessageView_Previews: PreviewProvider {
    static var previews: some View {
        MessageView()
    }
}
