AddResidentsView.swift 8.66 KB
//
//  AddResidentsView.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 14.06.2022.
//

import UIKit

protocol ViewDelegate: AnyObject {
    func viewDidTapImage()
}

class AddResidentsView: UIView {
    var isShow: Bool = false
    
    weak var delegate: ViewDelegate?
    
    let blurEffect: UIVisualEffectView = {
        let blur = UIBlurEffect(style: UIBlurEffect.Style.systemMaterialDark)
        let obj = UIVisualEffectView(effect: blur)
        obj.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        obj.alpha = 0.9
        obj.isUserInteractionEnabled = true
        return obj
    }()
    
    lazy var transparentView: UIView = {
        var obj = UIView()
        obj.isUserInteractionEnabled = true
        var tap = UIGestureRecognizer(target: self, action: #selector(imageTapped))
        obj.addGestureRecognizer(tap)
        obj.backgroundColor = .black
        obj.alpha = 0.5
        return obj
    }()
    
    var containerView: UIView = {
        var obj = UIView()
        obj.clipsToBounds = true
        obj.layer.cornerRadius = 15
        obj.backgroundColor = .white
        return obj
    }()
    
    var addResidentLabeL: UILabel = {
        var obj = UILabel()
        obj.text = "Add resident"
        obj.textColor = .TextColor.darkBlue
        obj.font = .skModernist(type: .bold, ofSize: 24)
        return obj
    }()
    
    var closeButton: UIButton = {
        var obj = UIButton()
        obj.layer.cornerRadius = 13
        obj.layer.borderWidth = 1
        obj.layer.borderColor = .ButtonBorderColor.lightGrey
        obj.setImage(UIImage(named: "Close"), for: .normal)
        return obj
    }()
    
    var dividingLineView: UIView = {
        var view = UIView()
        view.backgroundColor = MyColor.dividingLineBackgroundColor.value
        return view
    }()
    
    var residentNameLabel: UILabel = {
        var obj = UILabel()
        obj.text = "Resident name"
        obj.font = .skModernist(type: .bold, ofSize: 14)
        obj.textColor = .TextColor.darkBlue
        return obj
    }()
    
    var nameTextField: TextFieldWithPadding = {
        var obj = TextFieldWithPadding()
        obj.backgroundColor = .TextFieldColor.general
        obj.textAlignment = .left
        obj.layer.cornerRadius = 15
        obj.placeholder = "Enter resident name"
        obj.font = .skModernist(type: .regular, ofSize: 14)
        obj.textPadding = UIEdgeInsets(top: 0, left: 22, bottom: 0, right: 22)
        return obj
    }()
    
    var mobileNumberLabel: UILabel = {
        var obj = UILabel()
        obj.text = "Mobile number"
        obj.font = .skModernist(type: .bold, ofSize: 14)
        obj.textColor = MyColor.darkBlueTextColor.value
        return obj
    }()
    
    var mobileNumberTextField: TextFieldWithPadding = {
        var obj = TextFieldWithPadding()
        obj.backgroundColor = MyColor.generalTextFieldBackgroundColor.value
        obj.textAlignment = .left
        obj.layer.cornerRadius = 15
        obj.font = .skModernist(type: .regular, ofSize: 14)
        obj.textPadding = UIEdgeInsets(top: 0, left: 22, bottom: 0, right: 45)
        obj.placeholder = "Enter resident number"
        return obj
    }()
    
    let contactListButton: UIButton = {
        let obj = UIButton()
        obj.setImage(UIImage(named:"3Users"), for: .normal)
        return obj
    }()
    
    var managerButton: SquereCheckbox = {
        var obj = SquereCheckbox()
        obj.checkboxTitle = "Manager"
        return obj
    }()
    
    var hiddenButton: SquereCheckbox = {
        var obj = SquereCheckbox()
        obj.checkboxTitle = "Hidden"
        return obj
    }()
    
    var heartButton: UIButton = {
        var obj = UIButton()
        obj.setImage(UIImage(named: "Heart"), for: .normal)
        obj.setImage(UIImage(named: "HeartRed"), for: .selected)
        return obj
    }()
    
    var gradientBG: CAGradientLayer = {
        let obj = CAGradientLayer()
        var leftColor = UIColor(red: 0/255, green: 68/255, blue: 139/255, alpha: 1).cgColor
        var rightColor = UIColor(red: 0/255, green: 122/255, blue: 191/255, alpha: 1).cgColor
        obj.colors = [leftColor, rightColor]
        obj.startPoint = CGPoint(x: 0, y: 0.5)
        obj.endPoint = CGPoint(x: 1, y: 0.5)
        return obj
    }()
    
    var addResidentButton: UIButton = {
        var obj = UIButton()
        obj.setTitle("Add resident", for: .normal)
        obj.setTitleColor(UIColor(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
        obj.titleLabel?.font = .skModernist(type: .bold, ofSize: 16)
        obj.clipsToBounds = true
        obj.layer.masksToBounds = true
        return obj
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layout()
        backgroundColor = .clear
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func layout() {
        isUserInteractionEnabled = true
        addResidentButton.layer.insertSublayer(gradientBG, at: 0)
        
        addSubview(transparentView)
        transparentView.addSubview(blurEffect)
        
        addSubview(containerView)
        containerView.addSubview(addResidentLabeL)
        containerView.addSubview(closeButton)
        containerView.addSubview(dividingLineView)
        containerView.addSubview(residentNameLabel)
        containerView.addSubview(nameTextField)
        containerView.addSubview(mobileNumberLabel)
        containerView.addSubview(mobileNumberTextField)
        mobileNumberTextField.addSubview(contactListButton)
        
        containerView.addSubview(managerButton)
        containerView.addSubview(hiddenButton)
        containerView.addSubview(heartButton)
        containerView.addSubview(addResidentButton)
        
        transparentView.snp.makeConstraints {
            $0.leading.top.trailing.bottom.equalToSuperview()
        }
        
        containerView.snp.makeConstraints {
            $0.leading.trailing.equalToSuperview().inset(25)
            $0.centerY.equalTo(transparentView.snp.centerY)
        }
        
        addResidentLabeL.snp.makeConstraints {
            $0.top.equalToSuperview().offset(34)
            $0.leading.equalToSuperview().offset(28)
        }
        
        closeButton.snp.makeConstraints {
            $0.centerY.equalTo(addResidentLabeL)
            $0.trailing.equalToSuperview().offset(-30)
            $0.height.width.equalTo(45)
        }
        
        dividingLineView.snp.makeConstraints {
            $0.trailing.leading.equalToSuperview()
            $0.height.equalTo(1)
            $0.top.equalTo(addResidentLabeL.snp.bottom).offset(34)
        }
        
        residentNameLabel.snp.makeConstraints {
            $0.top.equalTo(dividingLineView.snp.bottom).offset(24)
            $0.leading.equalToSuperview().offset(44)
        }
        
        nameTextField.snp.makeConstraints {
            $0.leading.trailing.equalToSuperview().inset(30)
            $0.top.equalTo(residentNameLabel.snp.bottom).offset(10)
            $0.height.equalTo(47)
        }
        
        mobileNumberLabel.snp.makeConstraints {
            $0.leading.equalTo(residentNameLabel.snp.leading)
            $0.top.equalTo(nameTextField.snp.bottom).offset(15)
        }
        
        mobileNumberTextField.snp.makeConstraints {
            $0.leading.trailing.equalToSuperview().inset(30)
            $0.top.equalTo(mobileNumberLabel.snp.bottom).offset(10)
            $0.height.equalTo(47)
        }
        
        contactListButton.snp.makeConstraints {
            $0.height.width.equalTo(24)
            $0.centerY.equalToSuperview()
            $0.trailing.equalToSuperview().offset(-19)
        }
        
        managerButton.snp.makeConstraints {
            $0.leading.equalToSuperview().offset(29)
            $0.top.equalTo(mobileNumberTextField.snp.bottom).offset(30)
            $0.height.equalTo(20)
        }
        
        hiddenButton.snp.makeConstraints {
            $0.top.equalTo(managerButton)
            $0.leading.equalTo(managerButton.snp.trailing).offset(24)
            $0.height.equalTo(20)
        }
        
        heartButton.snp.makeConstraints {
            $0.top.equalTo(managerButton)
            $0.trailing.equalToSuperview().offset(-32.5)
        }
        
        addResidentButton.snp.makeConstraints {
            $0.top.equalTo(managerButton.snp.bottom).offset(43)
            $0.leading.trailing.bottom.equalToSuperview()
            $0.height.equalTo(64)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        gradientBG.frame = addResidentButton.bounds
        blurEffect.frame = transparentView.bounds
    }
    
    @objc func imageTapped() {
        delegate?.viewDidTapImage()
    }
}