//
//  GradientView.swift
//  Moodee
//
//  Created by Alexei Gudimenko on 8/12/17.
//  Copyright © 2017 Appetiser. All rights reserved.
//
//  ref: https://stackoverflow.com/questions/37038055/angled-gradient-layer-in-swift-2

import UIKit
import Foundation

@IBDesignable
class GradientView: UIView {
  
  private let gradientLayer = CAGradientLayer()
  
  @IBInspectable var cornerRadius: CGFloat = 0 {
    didSet {
      gradientLayer.cornerRadius = cornerRadius
      return layer.cornerRadius = cornerRadius
    }
  }
  
  @IBInspectable var startColor: UIColor = .white {
    didSet {
      configureGradient()
    }
  }
  
  @IBInspectable var endColor: UIColor = .blue {
    didSet {
      configureGradient()
    }
  }
  
  @IBInspectable var startPointX: CGFloat = 0 {
    didSet {
      configureGradient()
    }
  }
  
  @IBInspectable var startPointY: CGFloat = 0 {
    didSet {
      configureGradient()
    }
  }
  
  @IBInspectable var endPointX: CGFloat = 1 {
    didSet {
      configureGradient()
    }
  }
  
  @IBInspectable var endPointY: CGFloat = 1 {
    didSet {
      configureGradient()
    }
  }
  
  @IBInspectable var gradientLocationStart: Double = 0 {
    didSet {
      configureGradient()
    }
  }
  
  @IBInspectable var gradientLocationEnd: Double = 1 {
    didSet {
      configureGradient()
    }
  }
  
  /// Switch this on to control where within the frame the gradient starts taking effect,
  /// and where it stops. Otherwise the values entered in storyboard or set programmatically will be ignored. 
  @IBInspectable var usesGradientLocations: Bool = false {
    didSet {
      configureGradient()
    }
  }
  
  /// Switch this setting on to set specific value via storyboard. If this value
  /// is false, the default corner radius of this layer will be half of the layer's
  /// frame height.
  @IBInspectable var usesStoryboardCornerRadius: Bool = false {
    didSet {
      configureGradient()
    }
  }
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    configureGradient()
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    configureGradient()
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    gradientLayer.frame = bounds
    if usesStoryboardCornerRadius {
      // do nothing
    } else {
      // set corner radius to be half of frame height (i.e., evenly-rounded sides)
      gradientLayer.cornerRadius = gradientLayer.frame.size.height / 2
    }
  }
  
  private func configureGradient() {
    if let _ = layer.sublayers?[0] {
      layer.sublayers?.popLast()
    }
    
    print("*** gradient config called")
    if usesGradientLocations {
      let gStart = NSNumber(floatLiteral: gradientLocationStart)
      let gEnd = NSNumber(floatLiteral: gradientLocationEnd)
      gradientLayer.locations = [ gStart, gEnd ]
    }
    
    gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY)
    gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY)
    
    update(colors: [ startColor.cgColor, endColor.cgColor ])
    
    layer.insertSublayer(gradientLayer, at: 0)
    
    setNeedsDisplay()
  }
  
  func update(colors: [CGColor]) {
    gradientLayer.colors = colors
  }
}

