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

import UIKit

final class RemoveAdvertViewController: UIViewController {
    private let mainView = RemoveAdvertView()
    
    private let notification: NotificationManager
    private var storeKit: SubscriptionManager
    
    private let tableViewData = StringConstants.tableViewData
    
    init() {
        storeKit = SubscriptionManager()
        notification = NotificationManager()
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initViewController()
    }
    
    override func loadView() {
        view = mainView
    }
    
    private func initViewController() {
        addTargets()
        setupTableView()
    }
}

//MARK: - Action
extension RemoveAdvertViewController {
    @objc
    private func adBlockerButtonPressed(_ sender: UIButton) {
        if CachingManager.shared.adBlockerState {
            turnOffAdBlocker()
        } else {
            subscriptionHandler()
        }
       
    }
    
    @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 subscriptionHandler() {
        if storeKit.purchasedSubscriptions.isEmpty {
            Task {
                try? await storeKit.restorePurchase()
                
                if storeKit.purchasedSubscriptions.isEmpty {
                    presentSubscriptionHandler()
                } else {
                    if let restoreDate = CachingManager.shared.expirationDate {
                        if Date().timeIntervalSince1970 < restoreDate {
                            turnOnAdBlocker()
                        } else {
                            presentSubscriptionHandler()
                        }
                    } else {
                        DateManager.shared.getSubscriptionExpirationDate()
                        let restoreDate = CachingManager.shared.expirationDate
                        if Date().timeIntervalSince1970 < restoreDate ?? 0 {
                            turnOnAdBlocker()
                        } else {
                            presentSubscriptionHandler()
                        }
                    }
                }
            }
        } else {
            if let restoreDate = CachingManager.shared.expirationDate {
                if Date().timeIntervalSince1970 < restoreDate {
                    turnOnAdBlocker()
                } else {
                    presentSubscriptionHandler()
                }
            } else {
                DateManager.shared.getSubscriptionExpirationDate()
                let restoreDate = CachingManager.shared.expirationDate
                if Date().timeIntervalSince1970 < restoreDate ?? 0 {
                    turnOnAdBlocker()
                } else {
                    presentSubscriptionHandler()
                }
            }
        }
    }
    
    private func addTargets() {
        mainView.adBlockButton.addTarget(self, action:  #selector(adBlockerButtonPressed(_ :)), for: .touchUpInside)
        
        mainView.removeAdvertSegmentControl.addTarget(self, action: #selector(advertSegmantControlStateHandler), for: .valueChanged)
    }
    
    private func presentSubscriptionHandler() {
            let subcriptionViewController = SubscriptionViewController(storeKit: storeKit, complition: testHandler)
            subcriptionViewController.modalPresentationStyle = .fullScreen
            subcriptionViewController.modalTransitionStyle = .crossDissolve
            
            present(subcriptionViewController, animated: true)
    }
    
    private func testHandler() {
        let alert = UIAlertController(title: "Congradulations!", message: "Success by subscription", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default)
        alert.addAction(okAction)
        present(alert, animated: true)
        turnOnAdBlocker()
    }
    
    private func setupTableView() {
        mainView.adBlockTableView.dataSource = self
        mainView.adBlockTableView.delegate = self
        mainView.adBlockTableView.register(RemoveAdvertTableViewCell.self, forCellReuseIdentifier: RemoveAdvertTableViewCell.cellID)
    }
    
    private func turnOnAdBlocker() {
        mainView.adBlockButton.buttonImageView.image = .adBlockOnState
        mainView.tapActionLabel.textColor = ColorConstants.SearchbarTintBlue
        mainView.tapActionLabel.text = StringConstants.turnOff
        
        CachingManager.shared.adBlockerState = true
    }
    
    private func turnOffAdBlocker() {
        mainView.adBlockButton.buttonImageView.image = .adBlockOffState
        mainView.tapActionLabel.textColor = ColorConstants.customPink
        mainView.tapActionLabel.text = StringConstants.turnOn
        
        CachingManager.shared.adBlockerState = false
    }
}

//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
    }
}