//
//  DiscoverViewController.swift
//  wyy
//
//  Created by 施书顺 on 2021/3/5.
//

import UIKit
import SnapKit

class DiscoverViewController: UIViewController {
    
    let searchController = UISearchController()
    let leftNavItem = UIBarButtonItem(systemItem: .play)
    let rightNavItem = UIBarButtonItem(systemItem: .action)
    let newsView = DiscoverNewsView()
    let buttonsView = DiscoverButtonsView()
    
    let scrollView = UIScrollView()
    let contentView = UIView()
    let sideBar = SideBarView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        navigationItem.titleView = searchController.searchBar
        searchController.hidesNavigationBarDuringPresentation = false
        
//        navigationItem.setLeftBarButton(leftNavItem, animated: false)
        navigationItem.setRightBarButton(rightNavItem, animated: false)
//        leftNavItem.target = self
//        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(clickedOpenSideBar(sender:)))
        
        view.addSubview(scrollView)
//        scrollView.addSubview(contentView)
        
        scrollView.addSubview(newsView)
        newsView.snp.makeConstraints{ make in
            make.centerX.equalToSuperview()
            make.top.equalTo(10)
            make.width.equalTo(350)
            make.height.equalTo(200)
        }
        
        
        scrollView.addSubview(buttonsView)
        buttonsView.snp.makeConstraints{ make in
            make.centerX.equalToSuperview()
            make.top.equalTo(newsView.snp.bottom).offset(20)
            make.width.equalToSuperview()
            make.height.equalTo(80)
        }
        
        
        let line1 = UIView()
        line1.layer.borderWidth = 1.0
        line1.layer.borderColor = UIColor.black.withAlphaComponent(0.1).cgColor
        scrollView.addSubview(line1)
        line1.snp.makeConstraints{ make in
            make.top.equalTo(buttonsView.snp.bottom).offset(5)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(1)
        }
    
        let rcmdView = DiscoverRecommendView()
        scrollView.addSubview(rcmdView)
        rcmdView.snp.makeConstraints{ make in
            make.top.equalTo(line1.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(180)
        }
        
        let line2 = UIView()
        line2.backgroundColor = UIColor.gray.withAlphaComponent(0.1)
        scrollView.addSubview(line2)
        line2.snp.makeConstraints{ make in
            make.top.equalTo(rcmdView.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(10)
        }
        
        let popularSongView = DiscoverPopularSongsView()
        scrollView.addSubview(popularSongView)
        popularSongView.snp.makeConstraints{ make in
            make.top.equalTo(line2.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(250)
        }
        
        scrollView.frame = view.bounds
        scrollView.showsHorizontalScrollIndicator = true
        

//        scrollView.addSubview(sideBar)
//        sideBar.snp.makeConstraints{ make in
//            make.top.equalTo(0)
//            make.left.equalTo(-sideBar.frame.width)
//        }
        
    }
    
    override func viewDidLayoutSubviews() {
        let contentRect = scrollView.subviews.reduce(into: CGRect.zero, { rect, view in
            rect = rect.union(view.frame)
        })
        scrollView.contentSize = CGSize(width: contentRect.width, height: contentRect.height+60)
    }
    

    


}
