//
//  SettingsView.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 14/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct MeterSettingsView: View {
    
    @EnvironmentObject private var meter: Meter
    
    @State private var editingName = false
    @State private var editingScreenTimeout = false
    @State private var editingScreenBrightness = false

    var body: some View {
        ScrollView {
            VStack (spacing: 10) {
                // MARK: Name
                VStack {
                    HStack {
                        Text ("Name").fontWeight(.semibold)
                        Spacer()
                        if !editingName {
                            Text(meter.name)
                        }
                        ChevronView( rotate: $editingName )
                    }
                    if editingName {
                        EditNameView(editingName: $editingName, newName: meter.name)
                    }
                }
                .padding()
                .background(RoundedRectangle(cornerRadius: 15).foregroundColor(.secondary).opacity(0.1))
                if meter.operationalState == .dataIsAvailable {
                    // MARK: Screen Timeout
                    // Ar trebui separat enabled/disabled de valorile in minute eventual stocata valoarea in iCloud la dezactivare pentru restaurare
                    VStack{
                        HStack {
                            Text ("Screen Timeout").fontWeight(.semibold)
                            Spacer()
                            if !editingScreenTimeout {
                                Text ( meter.screenTimeout != 0 ? "\(meter.screenTimeout) Minutes" : "Off" )
                            }
                            ChevronView( rotate: $editingScreenTimeout )
                        }
                        if editingScreenTimeout {
                            EditScreenTimeoutView()
                        }
                    }
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 15).foregroundColor(.secondary).opacity(0.1))
                    // MARK: Screen Brightness
                    VStack{
                        HStack {
                            Text ("Screen Brightness").fontWeight(.semibold)
                            Spacer()
                            if !editingScreenBrightness {
                                Text ( "\(meter.screenBrightness)" )
                            }
                            ChevronView( rotate: $editingScreenBrightness )
                        }
                        if editingScreenBrightness {
                            EditScreenBrightnessView()
                        }
                    }
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 15).foregroundColor(.secondary).opacity(0.1))
                }
            }
        }
        .padding()
        .navigationBarTitle("Meter Settings")
        .navigationBarItems( trailing: RSSIView( RSSI: meter.btSerial.RSSI ).frame( width: 24 ) )
    }
}

struct EditNameView: View {
    
    @EnvironmentObject private var meter: Meter
    
    @Binding var editingName: Bool
    @State var newName: String
    
    var body: some View {
        TextField("Name", text: self.$newName, onCommit: {
            self.meter.name = self.newName
            self.editingName = false
        })
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .lineLimit(1)
            .disableAutocorrection(true)
            .multilineTextAlignment(.center)
            .padding(.horizontal)
    }
}

struct EditScreenTimeoutView: View {
    
    @EnvironmentObject private var meter: Meter
    
    var body: some View {
        Picker("", selection: self.$meter.screenTimeout ) {
            Text("1").tag(1)
            Text("2").tag(2)
            Text("3").tag(3)
            Text("4").tag(4)
            Text("5").tag(5)
            Text("6").tag(6)
            Text("7").tag(7)
            Text("8").tag(8)
            Text("9").tag(9)
            Text("Off").tag(0)
        }
        .pickerStyle( SegmentedPickerStyle() )
        .padding(.horizontal)
    }
}

struct EditScreenBrightnessView: View {
    
    @EnvironmentObject private var meter: Meter
    
    var body: some View {
        Picker("", selection: self.$meter.screenBrightness ) {
            Text("0").tag(0)
            Text("1").tag(1)
            Text("2").tag(2)
            Text("3").tag(3)
            Text("4").tag(4)
            Text("5").tag(5)
        }
        .pickerStyle( SegmentedPickerStyle() )
        .padding(.horizontal)
    }
}
