//
//  MyTabBarController.swift
//  BoxOffice
//
//  Created by Fury on 06/06/2019.
//  Copyright © 2019 Fury. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
    
    let movieTableVC = MovieTableViewController()
    let movieCollectionVC = MovieCollectionViewController()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let urlStr = "http://connect-boxoffice.run.goorm.io/movies?order_type=0"
        NetworkService.getData(urlStr) { (isTrue, movieData) in
            LoadingHUD.show()
            if isTrue {
                MovieDataManager.shared.allMovieData = movieData!
                LoadingHUD.hide()
                self.movieTableVC.movieTableView.reloadData()
            } else {
                
            }
        }
        
        movieTableVC.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 0)
        movieCollectionVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 1)
        
        self.delegate = self
        
        self.viewControllers = [
            UINavigationController(rootViewController: movieTableVC),
            UINavigationController(rootViewController: movieCollectionVC),
        ]
        
        let rightItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(rightBarButtonDidTapped(_:)))
        
        let rightItem2 = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(rightBarButtonDidTapped(_:)))

        movieTableVC.navigationItem.setRightBarButton(rightItem, animated: true)
        movieCollectionVC.navigationItem.setRightBarButton(rightItem2, animated: true)
      
        movieTableVC.navigationItem.title = "예매율순"
        movieCollectionVC.navigationItem.title = "예매율순"
    }

    @objc private func rightBarButtonDidTapped(_ sender: UIButton) {
        let alertController = UIAlertController(title: "정렬방식 선택", message: "영화를 어떤 순서로 정렬할까요?", preferredStyle: .actionSheet)
        
        let reservationAction = UIAlertAction(title: "예매율", style: .default) { (action) in
            let url = "http://connect-boxoffice.run.goorm.io/movies?order_type=0"
            self.getData(url)
            self.movieTableVC.navigationItem.title = "예매율순"
            self.movieCollectionVC.navigationItem.title = "예매율순"
        }
        
        let qrationAction = UIAlertAction(title: "큐레이션", style: .default) { (action) in
            let url = "http://connect-boxoffice.run.goorm.io/movies?order_type=1"
            self.getData(url)
            self.movieTableVC.navigationItem.title = "큐레이션순"
            self.movieCollectionVC.navigationItem.title = "큐레이션순"
        }
        
        let opendateAction = UIAlertAction(title: "개봉일", style: .default) { (action) in
            let url = "http://connect-boxoffice.run.goorm.io/movies?order_type=2"
            self.getData(url)
            self.movieTableVC.navigationItem.title = "개봉일순"
            self.movieCollectionVC.navigationItem.title = "개봉일순"
        }
        
        let cancelAction = UIAlertAction(title: "취소", style: .cancel)
        
        alertController.addAction(reservationAction)
        alertController.addAction(qrationAction)
        alertController.addAction(opendateAction)
        alertController.addAction(cancelAction)
        
        present(alertController, animated: true)
    }
    
    private func getData(_ url: String) {
        NetworkService.getData(url, completion: { (isTrue, movieData) in
            if isTrue {
                guard let movieData = movieData else { return }
                MovieDataManager.shared.allMovieData = movieData
                self.movieTableVC.movieTableView.reloadData()
                self.movieCollectionVC.movieCollectionView.reloadData()
                LoadingHUD.hide()
            } else {
                // 실패 시
            }
        })
    }
}
