RemoveAdvertViewController.swift 4.13 KB
//
//  RemoveAdvertViewController.swift
//  browser
//
//  Created by Artem Talko on 27.09.2023.
//

import UIKit

final class RemoveAdvertViewController: UIViewController {
    private let mainView = RemoveAdvertView()
    
    private let tableViewData = StringConstants.tableViewData
    
    var storeKit: StoreKitManager
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initViewController()
    }
    
    private func initViewController() {
        addTargets()
        setupTableView()
    }

    init() {
        self.storeKit = StoreKitManager()
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        view = mainView
    }
}

//MARK: - Action
extension RemoveAdvertViewController {
    @objc
    private func adBlockerButtonPressed(_ sender: UIButton) {
        viewAdBlockingHandler()
    }
    
    @objc
    private func advertSegmantControlStateHandler() {
        let browserHomeViewController = BrowserHomeViewController(url: nil, currentTabId: nil)
        let tabsViewController = TabsViewController()
        
        navigationController?.setViewControllers([tabsViewController, browserHomeViewController], animated: true)
    }
}

//MARK: Helpers
extension RemoveAdvertViewController {
    private func addTargets() {
        mainView.adBlockButton.addTarget(self, action:  #selector(adBlockerButtonPressed(_ :)), for: .touchUpInside)
        
        mainView.removeAdvertSegmentControl.addTarget(self, action: #selector(advertSegmantControlStateHandler), for: .valueChanged)
    }
    
    private func presentSubscriptionHandler() {
        if(storeKit.purchasedSubscriptions.isEmpty) {
            let subcriptionViewController = SubscriptionViewController()
            subcriptionViewController.modalPresentationStyle = .fullScreen
            subcriptionViewController.modalTransitionStyle = .crossDissolve
            
            present(subcriptionViewController, animated: true)
        }
    }
    
    private func setupTableView() {
        mainView.adBlockTableView.dataSource = self
        mainView.adBlockTableView.delegate = self
        mainView.adBlockTableView.register(RemoveAdvertTableViewCell.self, forCellReuseIdentifier: RemoveAdvertTableViewCell.cellID)
    }

    func successfullPurchaseHandler() {
        self.mainView.adBlockButton.buttonImageView.image = .adBlockOnState
        self.mainView.tapActionLabel.textColor = ColorConstants.SearchbarTintBlue
        self.mainView.tapActionLabel.text = StringConstants.turnOff
        
        CachingManager.shared.isAdBlocking = true
    }
    
    func viewAdBlockingHandler() {
        if CachingManager.shared.isAdBlocking {
            mainView.adBlockButton.buttonImageView.image = .adBlockOffState
            mainView.tapActionLabel.textColor = ColorConstants.customPink
            mainView.tapActionLabel.text = StringConstants.turnOn
            
            CachingManager.shared.isAdBlocking = false
        } else {
            if !storeKit.purchasedSubscriptions.isEmpty {
                mainView.adBlockButton.buttonImageView.image = .adBlockOnState
                mainView.tapActionLabel.textColor = ColorConstants.SearchbarTintBlue
                mainView.tapActionLabel.text = StringConstants.turnOff
                
                CachingManager.shared.isAdBlocking = true
            } else {
                presentSubscriptionHandler()
            }
        }
    }
}

//MARK: - Table View
extension RemoveAdvertViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: RemoveAdvertTableViewCell.cellID) as?
                RemoveAdvertTableViewCell else {
            return UITableViewCell()
        }
        cell.advantagesCellLabel.text = tableViewData[indexPath.row]

        return cell
    }
}