//
//  ScriptControllerIconView.swift
//  SBrowser
//
//  Created by James Magahern on 7/24/20.
//

import UIKit

class ScriptControllerIconView: ReliefButton
{
    public var shieldsDown: Bool = false {
        didSet { setNeedsLayout() }
    }
    
    @Invalidating(.layout)
    public var currentPolicy: ScriptPolicy = ScriptPolicy(securityOrigin: "", policyType: .alpha)
    
    private let labelView = UILabel(frame: .zero)
    private let policyImageView = UIImageView(frame: .zero)
    
    private let shieldsDownImage = UIImage(systemName: "shield.slash")
    private let shieldsUpImage = UIImage(systemName: "shield")
    private let shieldsPartiallyUpImage = UIImage(systemName: "shield.lefthalf.fill")
    
    override init() {
        super.init()
        
        addSubview(labelView)
        
        imageView?.contentMode = .scaleAspectFit
        labelView.backgroundColor = .systemRed
        labelView.textAlignment = .center
        labelView.layer.cornerRadius = 4.0
        labelView.layer.masksToBounds = true
        labelView.font = .boldSystemFont(ofSize: 8)
        labelView.textColor = .white
        
        policyImageView.contentMode = .scaleAspectFit
        
        setBlockedScriptsNumber(0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func setBlockedScriptsNumber(_ num: Int) {
        if num > 0 {
            labelView.isHidden = false
            labelView.text = "\(num)"
        } else {
            labelView.isHidden = true
        }
        
        setNeedsLayout()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        labelView.sizeToFit()
        labelView.center = CGPoint(x: bounds.center.x + 10, y: bounds.center.y + 10)
        labelView.bounds = labelView.bounds.insetBy(dx: -3.0, dy: -2.0)
        
        let policyImageWidth = round(bounds.width / 3)
        let policyImageSize = CGSize(
            // Make multiple of 2 to ensure proper centering
             width: policyImageWidth - policyImageWidth.truncatingRemainder(dividingBy: 2),
            height: policyImageWidth - policyImageWidth.truncatingRemainder(dividingBy: 2)
        )
        
        if let imageView = imageView {
            imageView.addSubview(policyImageView)
            policyImageView.frame = CGRect(origin: .zero, size: policyImageSize)
                .centered(inRect: imageView.bounds)
                .offsetBy(dx: 0.0, dy: -1.0)
        }
        
        if shieldsDown {
            setImage(shieldsDownImage, for: .normal)
        } else {
            setImage(shieldsUpImage, for: .normal)
            
            if isEnabled {
                policyImageView.isHidden = false
                policyImageView.image = ScriptPolicy.iconRepresentation(forPolicyType: currentPolicy.policyType, configuration: ScriptPolicy.IconConfiguration(
                    size: policyImageSize,
                    foregroundColor: .label,
                    backgroundColor: .clear
                ))
            } else {
                policyImageView.isHidden = true
            }
        }
    }
}
