RemoveAdvertView.swift 6.12 KB
//
//  RemoveAdvertView.swift
//  browser
//
//  Created by Artem Talko on 29.09.2023.
//

import UIKit
import SnapKit

final class RemoveAdvertView: UIView {
    let closeButton: UIButton = {
        let obj = UIButton()
        obj.setImage(UIImage(systemName: "xmark"), for: .normal)
        obj.contentMode = .scaleToFill
        obj.tintColor = .gray
        
        return obj
    }()
    
    let shieldView: UIButton = {
        let obj = UIButton()
        let userDefaultsValue = CachingManager.shared.isAdBlocking
        
        if userDefaultsValue {
            obj.setImage(UIImage(named: "ShieldActive"), for: .normal)
        } else {
            obj.setImage(UIImage(named: "ShieldInactive"), for: .normal)
        }
        obj.contentMode = .scaleAspectFit
        
        return obj
    }()
    
    private let gradientLayer = CAGradientLayer()
    
    private let abvertBlockerModeLabel: UILabel = {
        let obj = UILabel()
        obj.text = StringConstants.advertMode
        obj.font = FontConstants.semiboldFont_18
        
        return obj
    }()
    
    let tapActionLabel: UILabel = {
        let obj = UILabel()
        obj.text = StringConstants.turnOff
        obj.font = FontConstants.semiboldFont_14
        
        return obj
    }()
    
    let advantagesTableView: UITableView = {
        let obj = UITableView()
        obj.backgroundColor = .clear
        obj.isScrollEnabled = false
        obj.separatorStyle = .none
        
        return obj
    }()
    
    private let claimOfferLabel: UILabel = {
        let obj = UILabel()
        obj.text = StringConstants.claimOffer
        obj.font = FontConstants.regularFont_12
        
        return obj
    }()
    
    private let priceLabel: UILabel = {
        let obj = UILabel()
        obj.text = StringConstants.price
        obj.font = FontConstants.regularFont_18
        
        return obj
    }()
    
    private let subscribeButton: UIButton = {
        let obj = UIButton()
        obj.backgroundColor = .systemIndigo
        obj.setTitle(StringConstants.subscribe, for: .normal)
        obj.titleLabel?.font = FontConstants.semiboldFont_17
        obj.setTitleColor(.white, for: .normal)
        obj.layer.cornerRadius = 10
        obj.layer.masksToBounds = true
        
        return obj
    }()
    
    private let freeTrialLabel: UILabel = {
        let obj = UILabel()
        obj.textAlignment = .center
        obj.font = FontConstants.semiboldFont_12
        obj.text = StringConstants.freeTrial
        
        return obj
    }()
    
    private let discountLabel: UILabel = {
        let obj = UILabel()
        obj.textAlignment = .center
        obj.text = StringConstants.yourDiscount
        obj.font = FontConstants.regularFont_12
        
        return obj
    }()
    
    override init (frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        gradientLayer.frame = subscribeButton.bounds
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup() {
        addSubview(closeButton)
        addSubview(shieldView)
        addSubview(abvertBlockerModeLabel)
        addSubview(tapActionLabel)
        addSubview(advantagesTableView)
        addSubview(claimOfferLabel)
        addSubview(priceLabel)
        addSubview(subscribeButton)
        addSubview(freeTrialLabel)
        addSubview(discountLabel)
        
        setupConstraints()
        gradientSetup()
    }
    
    private func setupConstraints() {
        closeButton.snp.makeConstraints { make in
            make.top.equalTo(safeAreaLayoutGuide.snp.top).offset(16.sizeH)
            make.trailing.equalToSuperview().inset(16.sizeW)
            make.height.equalTo(24.sizeH)
        }
        
        shieldView.snp.makeConstraints { make in
            make.top.equalTo(closeButton.snp.top).offset(8.sizeH)
            make.leading.trailing.equalToSuperview().inset(47.sizeW)
            make.height.equalTo(280.sizeH)
        }
        
        abvertBlockerModeLabel.snp.makeConstraints { make in
            make.top.equalTo(shieldView.snp.bottom).offset(16.sizeH)
            make.centerX.equalToSuperview()
        }
        
        tapActionLabel.snp.makeConstraints { make in
            make.top.equalTo(abvertBlockerModeLabel.snp.bottom).offset(8.sizeH)
            make.centerX.equalToSuperview()
        }
        
        advantagesTableView.snp.makeConstraints { make in
            make.top.equalTo(tapActionLabel.snp.bottom).offset(26.sizeH)
            make.leading.equalToSuperview().offset(80.sizeW)
            make.bottom.equalTo(claimOfferLabel.snp.top).offset(-24.sizeH)
            make.trailing.equalToSuperview().offset(-86.sizeW)
        }
        
        claimOfferLabel.snp.makeConstraints { make in
            make.bottom.equalTo(priceLabel.snp.top).offset(-4.sizeH)
            make.centerX.equalToSuperview()
        }
        
        priceLabel.snp.makeConstraints { make in
            make.bottom.equalTo(subscribeButton.snp.top).offset(-16.sizeH)
            make.centerX.equalToSuperview()
        }
        
        subscribeButton.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(32.sizeW)
            make.bottom.equalTo(freeTrialLabel.snp.top).offset(-16.sizeH)
            make.height.equalTo(40.sizeH)
        }
        
        freeTrialLabel.snp.makeConstraints { make in
            make.bottom.equalTo(discountLabel.snp.top).offset(-8.sizeH)
            make.centerX.equalToSuperview()
        }
        
        discountLabel.snp.makeConstraints { make in
            make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom).offset(-24.sizeH)
            make.centerX.equalToSuperview()
        }
    }
}

//MARK: Gradient
extension RemoveAdvertView {
    private func gradientSetup() {
        subscribeButton.layer.insertSublayer(gradientLayer, at: 0)
        gradientLayer.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]
        gradientLayer.locations = [0.0, 1.0]
    }
}