//
//  KMPageRangeTools.swift
//  PDF Master
//
//  Created by tangchao on 2022/12/19.
//

import Cocoa

class KMPageRangeTools: NSObject {
    
    class func isValidPagesString(pagesString: String)-> Bool {
        var valid = false
        for ch in pagesString {
            if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" && ch != "," && ch != "-" {
                valid = false
                break
            } else {
                valid = true
            }
        }
        
        return valid
    }
    
    class func findSelectPage(pageRangeString: String, pageCount: Int) -> ([Int]) {
        if !isValidPagesString(pagesString: pageRangeString) {
            return []
        }
        
        var result: [Int] = []
        let array = pageRangeString.components(separatedBy: ",")
        for string in array {
            if string.isEmpty {
                return []
            } else {
                let pages = string .components(separatedBy: "-")
                if pages.count > 2 {
                    return []
                } else if pages.count == 1 {
                    let page = pages[0]
                    if page.isEmpty || Int(page)! > pageCount || Int(page)! == 0 {
                        return []
                    } else {
                        var hasSame: Bool = false
                        for i in result {
                            if i == Int(page)! {
                                hasSame = true
                                return []
                            }
                        }
                        if !hasSame {
                            result.append(Int(page)!)
                        }
                    }
                } else if pages.count == 2 {
                    let page1 = pages[0]
                    let page2 = pages[1]
                    if page1.isEmpty || page2.isEmpty || Int(page1)! >= Int(page2)! || Int(page2)! > pageCount || Int(page1)! == 0 {
                        return []
                    } else {
                        var hasSame: Bool = false
                        for i in Int(page1)! ... Int(page2)! {
                            for j in result {
                                if j == i {
                                    hasSame = true
                                    return []
                                }
                            }
                        }
                        if !hasSame {
                            for i in Int(page1)! ... Int(page2)! {
                                result.append(i)
                            }
                        }
                     }
                }
            }
        }
        
        return result
    }

    private class func parseSelectedIndexs(selectedIndex: Array<Int>) -> String {
        if (selectedIndex.count <= 0) {
            return ""
        }
        if (selectedIndex.count == 1) {
            return "\((selectedIndex.first)!)"
        }
        if (selectedIndex.count == 2) {
            return "\((selectedIndex.first)!),\((selectedIndex.last)!)"
        }
        
        // 元素大于2个
        var string: String = ""
        var index = 0
        var prePage = 0
        var count = 0
        var cubeStart = NSNotFound
        var cubeEnd = NSNotFound
        for i in selectedIndex {
            if index == 0 {
                string.append(String(i))
            } else {
                if i - prePage == 1 {
                    if count == 0 {
                        cubeStart = prePage
                    }
                    
                    count += 1
                } else {
                    count = 0
                }
                
                // 最后一个元素
                if (index == selectedIndex.count-1) {
                    if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
                        if cubeStart == selectedIndex.first {
                            string.removeFirst()
                        } else {
                            string.append(",")
                        }
                        
                        if index == selectedIndex.count-1 {
                            if count == 0 {
                                string.append(String(cubeStart))
                                string.append("-")
                                string.append(String(cubeEnd))
                                
                                string.append(",")
                                string.append(String(i))
                            } else {
                                string.append(String(cubeStart))
                                string.append("-")
                                string.append(String(i))
                            }
                        } else {
                            string.append(String(cubeStart))
                            string.append("-")
                            string.append(String(cubeEnd))
                        }
                    } else {
                        if prePage == selectedIndex.first {
                            
                        } else {
                            string.append(",")
                            string.append(String(prePage))
                        }
                        
                        cubeStart = NSNotFound
                        cubeEnd = NSNotFound
                    }
                    break
                }
                
                // 不是最后一个元素
                if count == 0 { /// 块结束
                    if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
                        if cubeStart == selectedIndex.first {
                            string.removeFirst()
                        } else {
                            string.append(",")
                        }
                        
                        
                        if index == selectedIndex.count-1 {
                            if count == 0 {
                                string.append(String(cubeStart))
                                string.append("-")
                                string.append(String(cubeEnd))
                                
                                string.append(",")
                                string.append(String(i))
                            } else {
                                string.append(String(cubeStart))
                                string.append("-")
                                string.append(String(i))
                            }
                        } else {
                            string.append(String(cubeStart))
                            string.append("-")
                            string.append(String(cubeEnd))
                        }
                    } else {
                        if prePage == selectedIndex.first {
                            //                                    string.append(",")
                        } else {
                            string.append(",")
                            string.append(String(prePage))
                        }
                        
                        cubeStart = NSNotFound
                        cubeEnd = NSNotFound
                    }
                } else if count >= 2 { /// 可成块
                    cubeEnd = i
                }
            }
            
            prePage = i
            index += 1
        }
        return string
    }
    
    class func newParseSelectedIndexs(selectedIndex: Array<Int>) -> String {
        if (selectedIndex.count == 0) {
            return ""
        }
        
        if (selectedIndex.count == 1) {
            return "\(selectedIndex.first!+1)"
        }
        
        var newDatas: Array<Int> = []
        for i in selectedIndex {
            newDatas.append(i)
        }
        /// 排序
        /// 根据id进行排序(升序)
        newDatas.sort(){$0 < $1}
        
        var a: Int = 0
        var b: Int = 0
        var result: String? = nil
        for i in newDatas {
            if (result == nil) {
                a = i
                b = i
                result = ""
            } else {
                if (i == b+1) {
                    b = i
                    if (i == newDatas.last) {
                        result!.append(String(format: "%d-%d", a+1,b+1))
                    }
                } else {
                    if (a == b) {
                        result!.append(String(format: "%d,", a+1))
                    } else {
                        result!.append(String(format: "%d-%d,", a+1,b+1))
                    }
                    a = i
                    b = i
                    if (i == newDatas.last) {
                        result!.append(String(format: "%d", a+1))
                    }
                }
            }
        }
        
        return result!
    }
}