//
//  LLQ_MutiWindowsController.swift
//  PrivateBrowser
//
//  Created by MAC on 2022/6/13.
//

import Foundation
import UIKit
class LLQ_WebListController : LLQ_BaseViewController, UICollectionViewDelegate,  UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    var viewModel: LLQ_HomeViewModel?
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
    fileprivate lazy var bgView: UIView = {
        let view = UIView.init(frame: .init(x: 0, y: 0, width: SCREEN_W, height: SCREEN_H))
        let bgIcon = UIImageView()
//        bgIcon.contentMode = .scaleAspectFill
        bgIcon.backgroundColor = .black
        view.addSubview(bgIcon)
        bgIcon.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(5)
        }
        view.backgroundColor = .black
        return view
    }()
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var cancelButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.set()
    }
    
    private func set(){
        self.showNavigationBar = false
        self.view.insertSubview(self.bgView, at: 0)
        bgView.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(5)
        }
        flowLayout.minimumLineSpacing = 17
        flowLayout.minimumInteritemSpacing = 17
        self.collectionView.contentInset = .init(top: 58, left: 24, bottom: 58, right: 24)
        self.collectionView.register(UINib.init(nibName: "LLQ_WebListCell", bundle: nil), forCellWithReuseIdentifier: "LLQ_WebListCell")

        
        self.cancelButton.rx.tap.subscribe {[weak self] _ in
            self?.navigationController?.popViewController(animated: true)
        }.disposed(by: disposeBg)
        
        self.addButton.rx.tap.subscribe {[weak self] _ in
            self?.viewModel?.resetHomePage()
            self?.navigationController?.popViewController(animated: true)
        }.disposed(by: disposeBg)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let models = viewModel?.viewModels else {
            return UICollectionViewCell()
        }
        let model = models[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LLQ_WebListCell", for: indexPath) as! LLQ_WebListCell
        cell.icon.image = model.snapScreenImage
        cell.closeBtn.rx.tap.subscribe { [weak self] _ in
            self?.deleteWeb(vm: model, indexPath: indexPath)
        }.disposed(by: disposeBg)
        return cell
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.viewModel?.viewModels.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (SCREEN_W - 24 * 2 - 17) / 2
        return .init(width: width, height: width / 155 * 200)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let vms = self.viewModel?.viewModels else {return}
        self.viewModel?.switchTo(singleVm: vms[indexPath.row])
        self.navigationController?.popViewController(animated: true)
        
    }
    
    
    private func deleteWeb(vm: LLQ_HomeSinglePageViewModel, indexPath: IndexPath){
        guard let models = self.viewModel?.viewModels else {return}
        if models.count == 1 {
            self.viewModel?.deleteWebPage(singleVm: vm)
            self.popViewController()
        } else {
            self.viewModel?.deleteWebPage(singleVm: vm)
            self.collectionView.reloadData()
        }
    }

}
    
