//
//  PostDetailsViewController.swift
//  JSONPlaceHolderApp
//
//  Created by Iván Díaz Molina on 16/2/21.
//  Copyright (c) 2021 IDIAZM. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit

protocol PostDetailsDisplayLogic: class {
    func setupView(viewModel: PostDetails.SetupView.ViewModel)
    func showLoading(_ show: Bool)
    func displayComments()
}

class PostDetailsViewController: BaseViewController, PostDetailsDisplayLogic {
    
    var interactor: PostDetailsBusinessLogic?
    var router: (NSObjectProtocol & PostDetailsRoutingLogic & PostDetailsDataPassing)?
    
    // MARK: IBOutlets
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var bodyLabel: UILabel!
    @IBOutlet weak var userLabel: UILabel!
    @IBOutlet weak var commentLabel: UILabel!
    @IBOutlet weak var commentsCollectionView: UICollectionView!
    
    // LETS AND VARS
    let CELL_MARGIN = CGFloat(8)
    let CELL_HEIGHT = CGFloat(300)
    let CELL_PERCENT = CGFloat(0.8)

    var itemWidth = CGFloat(0)
    
    // MARK: Object lifecycle
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    // MARK: Setup
    
    private func setup() {
        let viewController = self
        let interactor = PostDetailsInteractor()
        let presenter = PostDetailsPresenter()
        let router = PostDetailsRouter()
        viewController.interactor = interactor
        viewController.router = router
        interactor.presenter = presenter
        presenter.viewController = viewController
        router.viewController = viewController
        router.dataStore = interactor
    }
    
    // MARK: Routing
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let scene = segue.identifier {
            let selector = NSSelectorFromString("routeTo\(scene)WithSegue:")
            if let router = router, router.responds(to: selector) {
                router.perform(selector, with: segue)
            }
        }
    }
    
    // MARK: View lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        interactor?.setupView()
    }
    
    func setupView(viewModel: PostDetails.SetupView.ViewModel) {
        
        // navigation
        self.navigationItem.title = viewModel.navigationTitle
        
        // title
        titleLabel.text = viewModel.title
        titleLabel.numberOfLines = 0
        titleLabel.font = .boldSystemFont(ofSize: 22)
        
        // body
        bodyLabel.text = viewModel.body
        bodyLabel.font = .systemFont(ofSize: 18)
        bodyLabel.numberOfLines = 0
        
        // userName
        userLabel.text = viewModel.userName
        userLabel.textColor = .lightGray
        userLabel.font = .boldSystemFont(ofSize: 12)
        
        // comments
        commentLabel.text = "postDetails.comments.title".localized
        commentLabel.font = .boldSystemFont(ofSize: 18)
        
        // CollectionView
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // separacion externa collectionview
        layout.minimumLineSpacing = CELL_MARGIN // separacion interna entre items
        layout.minimumInteritemSpacing = CELL_MARGIN
        layout.scrollDirection = .horizontal
        
        commentsCollectionView.collectionViewLayout = layout
        
        commentsCollectionView.register(CommentCollectionViewCell.self)
        
        commentsCollectionView.delegate = self
        commentsCollectionView.dataSource = self
        commentsCollectionView.isPagingEnabled = false
        commentsCollectionView.isDirectionalLockEnabled = false
    }
    
    func showLoading(_ show: Bool) {
        displayLoading(show)
    }
    
    func displayComments() {
        reloadData(collectionView: commentsCollectionView)
    }
}

// MARK: UICollectionViewDelegate and UICollectionViewDataSource

extension PostDetailsViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return interactor?.getCommentsCount() ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CommentCollectionViewCell.cellIdentifier, for: indexPath) as? CommentCollectionViewCell else {
            print("Error to cast UICollectionViewCell to CommentCollectionViewCell")
            return UICollectionViewCell()
        }
                
        guard let data = interactor?.getCommentCellFor(index: indexPath.row) else {
            print("Error to get CommentCollectionViewCell from index")
            return UICollectionViewCell()
        }
        
        cell.updateUI(model: data)
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        itemWidth =  (commentsCollectionView.bounds.width - CELL_MARGIN) * CELL_PERCENT
        
        return CGSize(width: itemWidth, height: commentsCollectionView.bounds.height * 0.9)
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        setPosition(scrollView: scrollView)
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        setPosition(scrollView: scrollView)
    }
    
    private func setPosition(scrollView: UIScrollView) {
        
        let pageNumber = Int(ceil(scrollView.contentOffset.x + (commentsCollectionView.bounds.width / 2)) / (itemWidth + CELL_MARGIN))

        let indexPath = IndexPath(row: pageNumber, section: 0)
        commentsCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }
}
