//
//  AppDelegate.swift
//  iOSApp-Swift
//
//  Created by 聂高涛 on 2021/12/26.
//





import UIKit
@_exported import NXKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    open var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {        
        NXUI.darkMode = .light
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = NXNavigationController(rootViewController: EXMainViewController())
        self.window?.makeKeyAndVisible()
        
        let a = self.prefectValues([-5,-4,-3,-3,-2,-1,-1,0,0,0,0,1,2,3,3,4,5])
        print("count=\(a.count);\nvalue=\(a)")
                
        return true
    }
    
    
    
    func prefectValues(_ nums:[Int]) -> [[Int]] {
        let sortedNumbers = nums.sorted()
        var retValues = [[Int]]()
        
        for i in 0 ... sortedNumbers.count-3 {
            if sortedNumbers[i] >= 0 {
                return retValues
            }
            if i > 0 && sortedNumbers[i] == sortedNumbers[i-1] {
                continue
            }
            
            var left = i + 1
            var right = sortedNumbers.count - 1
            
            while left < right {
                let value = sortedNumbers[i] + sortedNumbers[left] + sortedNumbers[right]
                if value > 0 {
                    right  = right - 1
                }
                else if value < 0 {
                    left = left + 1
                }
                else {
                    retValues.append([sortedNumbers[i], sortedNumbers[left], sortedNumbers[right]])

                    while left < right && sortedNumbers[left] == sortedNumbers[left+1] {
                        left = left + 1
                    }
                    while left < right && sortedNumbers[right] == sortedNumbers[right-1] {
                        right = right - 1
                    }
                    left = left + 1
                    right  = right - 1
                }
                
            }
        }
        
        return retValues
    }
    
    func closeValues(_ nums:[Int], _ target:Int) -> [Int] {
        if nums.count < 3{
            return []
        }
        if nums.count == 3 {
            return [nums[0], nums[1], nums[2]]
        }
        
        
        let sortedNumbers = nums.sorted()
        var retValues = [sortedNumbers[0], sortedNumbers[1], sortedNumbers[2]]
        var retValue = retValues[0] + retValues[1] + retValues[2]

        for i in 0 ... sortedNumbers.count-1 {
            var loopValue = sortedNumbers[i]
            
            
        }
        return retValues
    }
}

