PayloadView.swift 5.63 KB
//
//  PayloadView.swift
//  browser
//
//  Created by Artem Talko on 25.09.2023.
//

import UIKit

final class PayloadView: GradientView {
    private let logoImageView: UIImageView = {
        let obj = UIImageView()
        obj.image = UIImage(named: "gotoweb")
        obj.contentMode = .scaleAspectFit
        
        return obj
    }()
    
    private let phoneImageView: UIImageView = {
        let obj = UIImageView()
        obj.image = UIImage(named: "Phone")
        obj.contentMode = .scaleAspectFit
        
        return obj
    }()
    
    private let zoomedTabImageView: UIImageView = {
        let obj = UIImageView()
        obj.image = UIImage(named: "ZoomedTab")
        obj.contentMode = .scaleAspectFit
        
        return obj
    }()
    
    let infoLabel: UILabel = {
        let obj = UILabel()
        obj.textColor = .white
        obj.text = StringConstants.ourBrowser
        obj.font = FontConstants.regularFont_12
        obj.numberOfLines = 0
        
        return obj
    }()
    
    let advantagesTableView: UITableView = {
        let obj = UITableView()
        obj.backgroundColor = .clear
        obj.isScrollEnabled = false
        obj.separatorStyle = .none
        
        return obj
    }()
    
    private let gradientButtonLayer = CAGradientLayer()
    
    let getStartedButton: UIButton = {
        let obj = UIButton()
        obj.setTitle("Get started", for: .normal)
        obj.titleLabel?.font = FontConstants.semiboldFont_17
        obj.setTitleColor(.white, for: .normal)
        obj.layer.cornerRadius = 10
        obj.layer.masksToBounds = true
        
        return obj
    }()

    let privacyLabelView: UILabel = {
        let obj = UILabel()
        obj.textAlignment = .center
        obj.textColor = .white
        let fullText = "By starting, you agree to our Terms and Condition and Privacy Policy."
        let attributedText = NSMutableAttributedString(string: fullText)
        
        attributedText.addAttribute(.foregroundColor, value: ColorConstants.customPinkForText.cgColor, range: NSRange(location: fullText.range(of: "Terms and Condition")?.lowerBound.utf16Offset(in: fullText) ?? 0, length: "Terms and Condition".count))
        attributedText.addAttribute(.foregroundColor, value: ColorConstants.customPinkForText.cgColor, range: NSRange(location: fullText.range(of: "Privacy Policy")?.lowerBound.utf16Offset(in: fullText) ?? 0, length: "Privacy Policy".count))
        obj.attributedText = attributedText
        obj.font = FontConstants.regularFont_12
        obj.numberOfLines = 0
        obj.isUserInteractionEnabled = true
        
        return obj
    }()
    
    override init (frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        gradientButtonLayer.frame = getStartedButton.bounds
    }
    
    private func setup() {
        addSubview(logoImageView)
        addSubview(phoneImageView)
        addSubview(zoomedTabImageView)
        addSubview(infoLabel)
        addSubview(advantagesTableView)
        addSubview(getStartedButton)
        addSubview(privacyLabelView)
        
        setupConstraints()
        gradientSetup()
    }
    
    private func setupConstraints() {
        logoImageView.snp.makeConstraints { make in
            make.top.equalTo(safeAreaLayoutGuide.snp.top).offset(16.sizeH)
            make.leading.equalToSuperview().offset(131.sizeW)
            make.height.equalTo(26.sizeH)
        }
        
        phoneImageView.snp.makeConstraints { make in
            make.top.equalTo(logoImageView.snp.bottom).offset(16.sizeH)
            make.leading.trailing.equalToSuperview().inset(90.sizeW)
            make.height.equalTo(355.sizeH)
        }
        
        zoomedTabImageView.snp.makeConstraints { make in
            make.top.equalTo(phoneImageView.snp.top).offset(119.sizeH)
            make.leading.equalTo(phoneImageView.snp.leading).offset(91.sizeW)
            make.bottom.equalTo(phoneImageView.snp.bottom).offset(-104.sizeH)
            make.trailing.equalToSuperview().offset(-71)
        }
        
        infoLabel.snp.makeConstraints { make in
            make.top.equalTo(phoneImageView.snp.bottom).offset(16.sizeH)
            make.leading.trailing.equalToSuperview().inset(55.sizeW)
        }
        
        advantagesTableView.snp.makeConstraints { make in
            make.top.equalTo(infoLabel.snp.bottom).offset(16.sizeH)
            make.leading.equalToSuperview().offset(55.sizeW)
            make.bottom.equalTo(getStartedButton.snp.top).offset(-38.sizeH)
            make.trailing.equalToSuperview().offset(-92.sizeW)
        }
        
        getStartedButton.snp.makeConstraints { make in
            make.top.equalTo(advantagesTableView.snp.bottom).offset(24.sizeH)
            make.leading.trailing.equalToSuperview().inset(32.sizeW)
            make.height.equalTo(40.sizeH)
        }
        
        privacyLabelView.snp.makeConstraints { make in
            make.top.equalTo(getStartedButton.snp.bottom).offset(16.sizeH)
            make.leading.trailing.equalToSuperview().inset(32.sizeW)
            make.bottom.equalTo(safeAreaLayoutGuide).offset(-16.sizeH)
        }
        
    }
}

//MARK: Gradients
extension PayloadView {
    private func gradientSetup() {
        getStartedButton.layer.insertSublayer(gradientButtonLayer, at: 0)
        gradientButtonLayer.colors = [UIColor(red: 0.349, green: 0.565, blue: 1, alpha: 1).cgColor, UIColor(red: 0.349, green: 0.565, blue: 1, alpha: 0.7).cgColor]
        gradientButtonLayer.locations = [0.0, 1.0]
    }
}