//
//  FindOnPageView.swift
//  App
//
//  Created by James Magahern on 9/30/20.
//

import UIKit

class FindOnPageView: UIView
{
    let textField = UISearchTextField(frame: .zero)
    
    let doneButton = ReliefButton()
    let nextResultButton = ReliefButton()
    let prevResultButton = ReliefButton()
    
    private let arrowControls = SegmentedReliefButton(children: [])
    
    let backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemChromeMaterial))
    
    convenience init() {
        self.init(frame: .zero)
        
        arrowControls.children = [ prevResultButton, nextResultButton ]
        
        addSubview(backgroundView)
        addSubview(textField)
        addSubview(doneButton)
        addSubview(arrowControls)
        
        textField.autocapitalizationType = .none
        textField.autocorrectionType = .no

        doneButton.setTitle("Done", for: .normal)
        doneButton.setTitleColor(.label, for: .normal)
        doneButton.constrainedToSquare = false
        
        nextResultButton.setImage(UIImage(systemName: "chevron.down"), for: .normal)
        prevResultButton.setImage(UIImage(systemName: "chevron.up"), for: .normal)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        backgroundView.frame = bounds
        
        let bounds = self.bounds
            .insetBy(dx: 8.0, dy: 8.0)
            .inset(by: safeAreaInsets)
        
        let doneButtonPadding: CGFloat = 8.0
        let doneButtonSize = doneButton.sizeThatFits(bounds.size)
        doneButton.frame = CGRect(
            x: bounds.width - doneButtonSize.width, y: bounds.minY,
            width: doneButtonSize.width + doneButtonPadding, height: bounds.height
        )
        
        let arrowControlsSize = arrowControls.sizeThatFits(bounds.size)
        arrowControls.frame = CGRect(
            x: doneButton.frame.minX - doneButtonPadding - arrowControlsSize.width, y: bounds.minY,
            width: arrowControlsSize.width, height: bounds.height
        )
        
        textField.frame = CGRect(
            x: bounds.minX, y: bounds.minY,
            width: arrowControls.frame.minX - bounds.minX - doneButtonPadding,
            height: bounds.height
        )
    }
}
