//
//  GraphView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 16.02.2021.
//

import UIKit
import BezierKit

class GraphView: UIView {
    //Private
    private let mainLineSettings = GraphLineSettings(lineWidth: 3,
                                                     dotRadius: 2.5,
                                                     dotLineWidth: 2,
                                                     color: ThemeManager.currentTheme.graphColor,
                                                     dotColor: ThemeManager.currentTheme.graphColor,
                                                     tintColor: ThemeManager.currentTheme.graphTintColor)
    private let additionalLineSettings = GraphLineSettings(lineWidth: 2,
                                                           dotRadius: 2,
                                                           dotLineWidth: 1,
                                                           color: UIColor(hex: 0xa4a4a4).withAlphaComponent(0.7),
                                                           dotColor: UIColor(hex: 0xa4a4a4).withAlphaComponent(0.7),
                                                           tintColor: UIColor(hex: 0x434343).withAlphaComponent(0.7))
    private let windLineSettings = GraphLineSettings(lineWidth: 3,
                                                     dotRadius: 2.5,
                                                     dotLineWidth: 2,
                                                     color: UIColor(hex: 0x8fc1ff),
                                                     dotColor: UIColor(hex: 0x2292fa),
                                                     tintColor: UIColor(hex: 0x2292fa))
    
    private lazy var mainLine:GraphLine = {
        let line = GraphLine(settings: mainLineSettings, onGetGraphRect: {
            return self.frame
        })
        
        return line
    }()
    private lazy var additionalLine:GraphLine = {
        let line = GraphLine(settings: additionalLineSettings, onGetGraphRect: {
            return self.frame
        })
        
        return line
    }()
    private lazy var windLine:GraphLine = {
        let line = GraphLine(settings: windLineSettings, onGetGraphRect: {
            return self.frame
        })
        
        return line
    }()
    
    //MARK:- View life cycle
    init() {
        super.init(frame: .zero)
        self.isUserInteractionEnabled = false
        
        //Adding lines
        layer.insertSublayer(mainLine.lineShape, at: 0)
        layer.insertSublayer(mainLine.tintLineShape, at: 1)
        
        layer.insertSublayer(additionalLine.lineShape, at: 0)
        layer.insertSublayer(additionalLine.tintLineShape, at: 0)
        
        layer.insertSublayer(windLine.lineShape, at: 0)
        layer.insertSublayer(windLine.tintLineShape, at: 1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //Public
    public func drawMainGraph(with points:[CGPoint]) {
        mainLine.updateWith(points: points)
        mainLine.lineDots.forEach {
            layer.addSublayer($0.shape)
        }
    }
    
    public func drawAdditionalGraph(with points:[CGPoint]) {
        additionalLine.updateWith(points: points)
        additionalLine.lineDots.forEach {
            layer.addSublayer($0.shape)
        }
    }
    
    public func drawWindGraph(with points:[CGPoint]) {
        windLine.updateWith(points: points)
        windLine.lineDots.forEach {
            layer.addSublayer($0.shape)
        }
    }
    
    public func tintGraphFrom(startPointX:CGFloat, endPointX:CGFloat) {
        mainLine.tintLineFrom(startPointX: startPointX, to: endPointX)
        additionalLine.tintLineFrom(startPointX: startPointX, to: endPointX)
        windLine.tintLineFrom(startPointX: startPointX, to: endPointX)
    }
    
    public func tintMainDotAt(point: CGPoint) {
        mainLine.tintDotAt(point: point)
        windLine.tintDotAt(point: point)
    }
    
    public func tintAdditionalDotAt(point: CGPoint) {
        additionalLine.tintDotAt(point: point)
    }
}
