//
//  LLQ_HomeController.swift
//  PrivateBrowser
//
//  Created by MAC on 2022/6/9.
//

import UIKit
import RxSwift

class LLQ_HomeController : LLQ_BaseViewController {
    @IBOutlet weak var settingBtn: UIButton!
    @IBOutlet weak var navView: UIView!
    @IBOutlet weak var bottomBg: UIImageView!
    @IBOutlet weak var textViewBg: UIView!
    @IBOutlet weak var textView: LLQ_SearchTextView!
    @IBOutlet weak var textViewTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var textViewHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var itemsView: UIStackView!
    @IBOutlet weak var blurView: UIVisualEffectView!
    @IBOutlet weak var webContentView: LLQ_HomeWebControlView!
    @IBOutlet weak var bottomBar: LLQ_HomeBottomBarView!
    
    @IBOutlet weak var navimg: UIImageView!
    
    
    var viewModel = LLQ_HomeViewModel.init()
    var style: HomePageStyle = .idle
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.webContentView.viewISApearing = true
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.webContentView.viewISApearing = false
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.set()
        navimg.layer.cornerRadius = 10
    }
    
    public func initializeShowHomePage(){
        
    }
    
    private func set(){
        self.showNavigationBar = false
        self.textView.setViewModel(viewModel: self.viewModel)
        self.webContentView.setViewModel(viweModel: self.viewModel)
        // change ui
        let changeUIBinder = Binder<HomePageStyle>.init(self, scheduler: MainScheduler.asyncInstance, binding: { controller, style in
            if style == .idle {
                self.animateToDimissEditSearch()
            } else if  style == .editingSearch {
                controller.animateToShowEditSearch()
            } else if style == .searched {
                controller.aniamteToSearchedView()
            }
            controller.style = style
        })
        self.viewModel.styleStream.asObservable().distinctUntilChanged().bind(to: changeUIBinder).disposed(by: self.disposeBg)
        
        
        // setting
        let settingPushBinder = Binder<Void>.init(self, scheduler: MainScheduler.asyncInstance) { controller, _ in
            let settingVc = UIStoryboard.main().initController(id: "LLQ_SettingController") as! LLQ_SettingController
            controller.navigationController?.pushViewController(settingVc, animated: true)
        }
        self.settingBtn.rx.tap.bind(to: settingPushBinder).disposed(by: self.disposeBg)
        
        // bottomBar
        self.bottomBar.previousButton.isEnabled = false
        self.bottomBar.forwardButton.isEnabled = false
        self.bottomBar.homeButton.isEnabled = false
        self.bottomBar.windowsButton.rx.tap.subscribe { [weak self] event in
            self?.webContentView.doSnapIfNeeded()
            self?.toMutiWindows()
        }.disposed(by: self.disposeBg)
        
        self.viewModel.mutiWinowEnableStream.asObservable().bind(to: self.bottomBar.windowsButton.rx.isEnabled).disposed(by: disposeBg)
        
        // to list vc
        self.viewModel.toListControllerStream.asObservable().subscribe {[weak self] _ in
            self?.toMutiWindows()
        }.disposed(by: disposeBg)
        
        // tap blur view
        let tap = UITapGestureRecognizer.init()
        tap.rx.event.subscribe {[weak self] _ in
            if self?.viewModel.styleStream.value == .editingSearch {
                self?.viewModel.editingStream.accept(false)
                self?.viewModel.resetHomePage()
            }
        }.disposed(by: disposeBg)
        self.blurView.addGestureRecognizer(tap)
    }
    
    private func animateToDimissEditSearch(){
        self.view.layoutIfNeeded()
        self.textViewHeightConstraint.constant = 47
        self.textViewTopConstraint.constant = 72
        self.navView.isHidden = false
        self.itemsView.isHidden = false
        self.bottomBar.isHidden = false
        
        UIView.animate(withDuration: 0.25, delay: 0, options: UIView.AnimationOptions.curveEaseOut) {
            self.navView.alpha = 1
            self.blurView.alpha = 0
            self.itemsView.alpha = 1
            self.textViewBg.alpha = 0
            self.webContentView.alpha = 0
            self.bottomBar.alpha = 1
            self.view.layoutIfNeeded()
        } completion: { _ in
            self.blurView.isHidden = true
            self.textViewBg.isHidden = true
            self.webContentView.isHidden = true

        }
    }
    
    private func animateToShowEditSearch(){
        self.view.layoutIfNeeded()
        self.textViewHeightConstraint.constant = 44
        self.textViewTopConstraint.constant = 13.5
        self.blurView.isHidden = false
        self.itemsView.isHidden = false
        UIView.animate(withDuration: 0.25, delay: 0, options: UIView.AnimationOptions.curveEaseOut) {
            self.navView.alpha = 0
            self.blurView.alpha = 1
            self.itemsView.alpha = 1
            self.textViewBg.alpha = 0
            self.webContentView.alpha = 0
            self.bottomBar.alpha = 0
            self.view.layoutIfNeeded()
        } completion: { _ in
            self.navView.isHidden = true
            self.textViewBg.isHidden = true
            self.webContentView.isHidden = true
            self.bottomBar.isHidden = true
        }
    }
    
    private func aniamteToSearchedView(){
        self.view.layoutIfNeeded()
        self.textViewHeightConstraint.constant = 44
        self.textViewTopConstraint.constant = 13.5
        self.blurView.isHidden = false
        self.textViewBg.isHidden = false
        self.webContentView.isHidden = false
        UIView.animate(withDuration: 0.25, delay: 0, options: UIView.AnimationOptions.curveEaseOut) {
            self.navView.alpha = 0
            self.blurView.alpha = 0
            self.itemsView.alpha = 0
            self.textViewBg.alpha = 1
            self.webContentView.alpha = 1
            self.bottomBar.alpha = 0

            self.view.layoutIfNeeded()
        } completion: { _ in
            self.navView.isHidden = true
            self.blurView.isHidden = true
            self.itemsView.isHidden = true
            self.webContentView.isHidden = false
            self.bottomBar.isHidden = true
        }
    }
    
    @IBAction func responseToInstagram(_ sender: Any) {
        self.viewModel.openWeb(type: .Instagram)
    }
    
    @IBAction func responseToVimor(_ sender: Any) {
        self.viewModel.openWeb(type: .Vimeo)
    }
    
    @IBAction func responseToTikTok(_ sender: Any) {
        self.viewModel.openWeb(type: .Tiktok)
    }
    
    @IBAction func responseToFaceBook(_ sender: Any) {
        self.viewModel.openWeb(type: .Facebook)
    }
    
    private func toMutiWindows(){
        guard self.viewModel.viewModels.count > 0 else {
            return
        }
        let listVc = UIStoryboard.main().initController(id: "LLQ_WebListController") as! LLQ_WebListController
        listVc.viewModel = self.viewModel
        self.navigationController?.pushViewController(listVc, animated: true)
    }
    
    
}
