//
//  ContentView.swift
//  Toolboxes
//
//  Created by 研发2组 on 2024/9/21.
//

import SwiftUI

@available(iOS 16.0, *)
struct ContentView: View {
    
    @State private var showOverlay = false
    @State private var selectedTab: Int = 0
    @State private var showRatingAlert = false
//    @State private var _showHorizontalGlobe = State<Bool>(initialValue: {
//          if let show = UserDefaults.standard.object(forKey: "showHorizontalGlobe") as? Bool {
//              return show
//          } else {
//              return true
//          }
//      }())
//        
//      @State private var _showAuxiliaryLine = State<Bool>(initialValue: {
//          if let show = UserDefaults.standard.object(forKey: "showAuxiliaryLine") as? Bool {
//              return show
//          } else {
//              return true
//          }
//      }())
//        
//      @State private var _showCompass = State<Bool>(initialValue: {
//          if let show = UserDefaults.standard.object(forKey: "showCompass") as? Bool {
//              return show
//          } else {
//              return true
//          }
//      }())  
    init() {
           // 设置默认值
           let defaults = UserDefaults.standard
             
           // 检查并设置 showHorizontalGlobe
           if defaults.object(forKey: "showHorizontalGlobe") == nil {
               defaults.setValue(true, forKey: "showHorizontalGlobe")
           }
         
             
           // 检查并设置 showAuxiliaryLine
           if defaults.object(forKey: "showAuxiliaryLine") == nil {
               defaults.setValue(true, forKey: "showAuxiliaryLine")
           }
          
             
           // 检查并设置 showCompass
           if defaults.object(forKey: "showCompass") == nil {
               defaults.setValue(true, forKey: "showCompass")
           }
       
       }
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                BarView(title: "Compass", imagename: "tab_compass", selectedImageName: "tab_compass_sel", tabIndex: 0, selectedTab: $selectedTab) {
                    
                    CompassView(showPopup: $showOverlay)
                }
                .tag(0)
                
                BarView(title: "Data", imagename: "tab_data", selectedImageName: "tab_data_sel", tabIndex: 1, selectedTab: $selectedTab) {
                    DataView()
                }
                .tag(1)
                
                BarView(title: "Location", imagename: "tab_loc", selectedImageName: "tab_loc_sel", tabIndex: 2, selectedTab: $selectedTab) {
                    LocationView()
                }
                .tag(2)
                
                BarView(title: "More", imagename: "tab_more", selectedImageName: "tab_more_sel", tabIndex: 3, selectedTab: $selectedTab) {
                    MoreView()
                }
                .tag(3)
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())  // 无论设备如何，都保持一致
        .navigationBarTitle("")
        .onAppear(){
            
            RateTool.check { isSuccess in
                RateTool.fetchConfig { success in
                    if success {
                        showRateAlert()
                    }
                }
            }
            MapViewManager.shared.load()
        }
        .overlay{
            Group {
                if showOverlay {
                    
                    PopupView(isShowing: $showOverlay)
                        .ignoresSafeArea()
                }
                
                if showRatingAlert{
                    RatingAlertView(isPresented: $showRatingAlert)
                }
            }
        }
        
    }
    
    /// 执行第二天的方法（显示评价弹窗）
    private func executeMethodOnSecondDay() {
        // 显示评价弹窗
        showRatingAlert = true;
    }
    
    /// 显示评价弹窗（带判断逻辑）
    private func showRateAlert() {
        // 检查是否已经显示过评价弹窗
        if UserDefaults.standard.bool(forKey: "eva_isshowed") {
            return
        }
        executeMethodOnSecondDay()
       
    }
}

// 自定义 TabBar 项目
struct BarView<Content: View>: View {
    var title: String
    var imagename: String
    var selectedImageName: String
    var tabIndex: Int
    @Binding var selectedTab: Int
    var content: Content
    
    init(title: String, imagename: String, selectedImageName: String, tabIndex: Int, selectedTab: Binding<Int>, @ViewBuilder content: () -> Content) {
        self.title = title
        self.imagename = imagename
        self.selectedImageName = selectedImageName
        self.tabIndex = tabIndex
        self._selectedTab = selectedTab
        self.content = content()
    }
    
    var body: some View {
        content
            .tabItem {
                BarItem(title: title, imagename: selectedTab == tabIndex ? selectedImageName : imagename)
            }
            .tag(tabIndex)
    }
}

// 自定义 TabBar 项目的图标和文字视图
struct BarItem: View {
    let title: String
    let imagename: String
    
    var body: some View {
        VStack {
            Image(imagename)
                .resizable() // 允许图片调整大小
                .aspectRatio(contentMode: .fit) // 保持图片比例
                .frame(width: 30,height: 30)
            Text(L(title)).font(.subheadline)
        }
    }
}

@available(iOS 16.0, *)
struct ContentView_Previews: PreviewProvider {
    
    static var previews: some View {
        ContentView()
        
    }
}

