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

import SwiftUI

struct CanStatusView: View {
    
    @StateObject var viewModel = CanStatusViewModel()
    
    let refresh: () -> Void
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            GeometryReader { proxy in
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "캔내역") {
                        AppState.shared.setAppStep(step: .main)
                    }
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        VStack(spacing: 26.7) {
                            HStack(spacing: 6.7) {
                                Image("ic_can")
                                    .resizable()
                                    .frame(width: 26.7, height: 26.7)
                                
                                Text("\(viewModel.totalCan) 캔")
                                    .font(.custom(Font.bold.rawValue, size: 18.3))
                                    .foregroundColor(Color(hex: "eeeeee"))
                            }
                            
                            HStack(spacing: 26.7) {
                                VStack(spacing: 10) {
                                    Text("결제 캔")
                                        .font(.custom(Font.light.rawValue, size: 12))
                                        .foregroundColor(Color(hex: "777777"))
                                    
                                    HStack(alignment: .bottom, spacing: 3.3) {
                                        Text("\(viewModel.chargeCan)")
                                            .font(.custom(Font.bold.rawValue, size: 16.7))
                                            .foregroundColor(Color(hex: "eeeeee"))
                                        
                                        Text(" 캔")
                                            .font(.custom(Font.medium.rawValue, size: 10.7))
                                            .foregroundColor(Color(hex: "bbbbbb"))
                                    }
                                }
                                .padding(.horizontal, 6.7)
                                
                                Rectangle()
                                    .frame(width: 1, height: 26.7)
                                    .foregroundColor(Color(hex: "909090").opacity(0.5))
                                
                                VStack(spacing: 10) {
                                    Text("리워드 캔")
                                        .font(.custom(Font.light.rawValue, size: 12))
                                        .foregroundColor(Color(hex: "777777"))
                                    
                                    HStack(alignment: .bottom, spacing: 3.3) {
                                        Text("\(viewModel.rewardCan)")
                                            .font(.custom(Font.bold.rawValue, size: 16.7))
                                            .foregroundColor(Color(hex: "eeeeee"))
                                        
                                        Text(" 캔")
                                            .font(.custom(Font.medium.rawValue, size: 10.7))
                                            .foregroundColor(Color(hex: "bbbbbb"))
                                    }
                                }
                                .padding(.horizontal, 6.7)
                            }
                        }
                        .padding(.vertical, 13.3)
                        .frame(width: screenSize().width - 26.7)
                        .background(Color(hex: "222222"))
                        .cornerRadius(16.7)
                        .padding(.top, 13.3)
                        
                        HStack(spacing: 0) {
                            VStack(spacing: 0) {
                                Spacer()
                                Text("충전내역")
                                    .font(.custom(Font.medium.rawValue, size: 13.3))
                                    .foregroundColor(
                                        Color(hex: viewModel.currentTab == .charge ? "eeeeee" : "777777")
                                    )
                                Spacer()
                                Rectangle()
                                    .frame(height: 1)
                                    .foregroundColor(
                                        Color(hex: "fdca2f")
                                            .opacity(viewModel.currentTab == .charge ? 1 : 0)
                                    )
                            }
                            .frame(width: screenSize().width / 2, height: 50)
                            .onTapGesture {
                                if viewModel.currentTab != .charge {
                                    viewModel.currentTab = .charge
                                }
                            }
                            
                            VStack(spacing: 0) {
                                Spacer()
                                Text("사용내역")
                                    .font(.custom(Font.medium.rawValue, size: 13.3))
                                    .foregroundColor(
                                        Color(hex: viewModel.currentTab == .use ? "eeeeee" : "777777")
                                    )
                                Spacer()
                                Rectangle()
                                    .frame(height: 1)
                                    .foregroundColor(
                                        Color(hex: "fdca2f")
                                            .opacity(viewModel.currentTab == .use ? 1 : 0)
                                    )
                            }
                            .frame(width: screenSize().width / 2, height: 50)
                            .onTapGesture {
                                if viewModel.currentTab != .use {
                                    viewModel.currentTab = .use
                                }
                            }
                        }
                        .padding(.top, 13.3)
                        
                        switch viewModel.currentTab {
                        case .charge:
                            CanChargeStatusView()
                            
                        case .use:
                            CanUseStatusView()
                        }
                    }
                    
                    Spacer()
                    
                    HStack(spacing: 6.7) {
                        Image("ic_can")
                            .resizable()
                            .frame(width: 26.7, height: 26.7)
                        
                        Text("충전하기")
                            .font(.custom(Font.bold.rawValue, size: 18.3))
                            .foregroundColor(Color(hex: "1313bc"))
                    }
                    .padding(.vertical, 16)
                    .frame(width: screenSize().width - 26.7)
                    .background(Color(hex: "80d8ff"))
                    .cornerRadius(10)
                    .padding(.vertical, 13.7)
                    .frame(width: screenSize().width)
                    .background(Color(hex: "222222"))
                    .cornerRadius(16.7, corners: [.topLeft, .topRight])
                    .onTapGesture {
                        AppState.shared.setAppStep(step: .canCharge(refresh: refresh))
                    }
                    
                    if proxy.safeAreaInsets.bottom > 0 {
                        Rectangle()
                            .foregroundColor(Color(hex: "222222"))
                            .frame(width: proxy.size.width, height: 15.3)
                    }
                }
                .edgesIgnoringSafeArea(.bottom)
            }
        }
        .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()
                }
            }
        }
        .onAppear {
            viewModel.getCanStatus()
        }
    }
}

struct CanStatusView_Previews: PreviewProvider {
    static var previews: some View {
        CanStatusView {}
    }
}