import UIKit

class FlagLayer: CALayer {
	let pixelWidth = 1/UIScreen.main.scale
	
	// Flag colors - https://ru.wikipedia.org/wiki/%D0%A4%D0%BB%D0%B0%D0%B3_%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D0%B8
	let blue = CGColor(srgbRed: 0, green: 57/256.0, blue: 166/256.0, alpha: 1)
	let red = CGColor(srgbRed: 213/256.0, green: 43/256.0, blue: 30/256.0, alpha: 1)
	
	override func draw(in ctx: CGContext) {
		ctx.saveGState()
		super.draw(in: ctx)
		
		ctx.setStrokeColor(UIColor.black.cgColor)
		ctx.setLineWidth(0)
		
		ctx.setFillColor(UIColor.white.cgColor)
		ctx.fill(bounds.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: bounds.height*2/3, right: 0)))
		
		ctx.setFillColor(self.blue)
		ctx.fill(bounds.insetBy(dx: 0, dy: bounds.height/3))
		
		ctx.setFillColor(self.red)
		ctx.fill(bounds.inset(by: UIEdgeInsets(top: bounds.height*2/3, left: 0, bottom: 0, right: 0)))
		
		ctx.setLineWidth(pixelWidth)
		ctx.stroke(bounds.insetBy(dx: pixelWidth/2, dy: pixelWidth/2))
		
		ctx.restoreGState()
	}
}
