//
//  ExploreWokaVC.swift
//  WOKA
//
//  Created by MacBook Pro on 21/05/24.
//

import UIKit

class ExploreWokaVC: UIViewController {

    @IBOutlet weak var blurView: UIView!
    @IBOutlet weak var extraView: UIView!
    @IBOutlet weak var customView: UIView!
    
    @IBOutlet weak var wokaFmBtn: UIButton!
    @IBOutlet weak var liveTvBtn: UIButton!
    @IBOutlet weak var webSeriesBtn: UIButton!
    @IBOutlet weak var gamesBtn: UIButton!
    @IBOutlet weak var audioBookBtn: UIButton!
    @IBOutlet weak var blogBtn: UIButton!
    @IBOutlet weak var karaokeBtn: UIButton!
    @IBOutlet weak var shopBtn: UIButton!
    
    @IBOutlet weak var layerView: UIView!
    @IBOutlet weak var bottomView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        drawBackgroundBlur()
    }
    
    override func viewDidLayoutSubviews() {
//        customView.drawCustomShape()
    }
    
    func drawBackgroundBlur() {
        let blurEffect = UIBlurEffect(style: .light)
        let blurredView = UIVisualEffectView(effect: blurEffect)
        blurredView.frame = self.view.bounds
        blurredView.alpha = 0.95
        self.extraView.addSubview(blurredView)
        
        let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
        let vibrancyView = UIVisualEffectView(effect: vibrancyEffect)
        vibrancyView.translatesAutoresizingMaskIntoConstraints = false
        vibrancyView.contentView.addSubview(blurView)
        blurredView.contentView.addSubview(vibrancyView)
    }
    
    @IBAction func btnTapped(_ sender: UIButton) {
        switch sender{
        case wokaFmBtn:
            PersistentStorage.shared.addRadioCount()
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.radio])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.radio])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.radio])
                }
            }
        case webSeriesBtn:
            PersistentStorage.shared.addOthersCount()
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.webseries])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.webseries])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.webseries])
                }
            }
        case gamesBtn:
            PersistentStorage.shared.addOthersCount()
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.games])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.games])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.games])
                }
            }
        case audioBookBtn:
            PersistentStorage.shared.addOthersCount()
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.audioBooks])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.audioBooks])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.audioBooks])
                }
            }
        case karaokeBtn:
            PersistentStorage.shared.addOthersCount()
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.karaoke])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.karaoke])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.karaoke])
                }
            }
        case shopBtn:
            PersistentStorage.shared.addOthersCount()
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.shop])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.shop])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.shop])
                }
            }
        case liveTvBtn:
            PersistentStorage.shared.addLiveTVCount()
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.liveTV])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.liveTV])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.liveTV])
                }
            }
        case blogBtn:
            self.dismiss(animated: true) {
                switch K.GVar.topView{
                case .theme1:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeOnePush), object: nil, userInfo: ["action": TopViewPush.blogs])
                case .theme2:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.themeTwoPush), object: nil, userInfo: ["action": TopViewPush.blogs])
                case .myList:
                    NotificationCenter.default.post(name: NSNotification.Name(K.NotificationCenterReloads.myListPush), object: nil, userInfo: ["action": TopViewPush.blogs])
                }
            }
        default:
            break
        }
    }
    
    
    @IBAction func closeTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        self.dismiss(animated: true)
    }
}

extension  UIView{
    func drawCustomShape() {
        let cornerRadius: CGFloat = 18.0
        let shapeOffset = self.frame.size.height * 0.2
        let triangleHeight: CGFloat = 20.0  // height of the triangle pointer
        let triangleWidth: CGFloat = 40.0  // width of the triangle pointer

        // create shape layer
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = self.bounds
        shapeLayer.lineWidth = 1.0
        shapeLayer.fillColor = UIColor.white.cgColor

        self.layer.addSublayer(shapeLayer)

        // create path
        let path = UIBezierPath()

        // top left point
        path.move(to: CGPoint(x: 0, y: cornerRadius))
        // top left corner
        path.addQuadCurve(to: CGPoint(x: cornerRadius, y: 0), controlPoint: CGPoint(x: 0, y: 0))

        // top right point
        path.addLine(to: CGPoint(x: self.frame.size.width - cornerRadius, y: 0))
        // top right corner
        path.addQuadCurve(to: CGPoint(x: self.frame.size.width, y: cornerRadius), controlPoint: CGPoint(x: self.frame.size.width, y: 0))

        // bottom right point before corner
        path.addLine(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height - shapeOffset - cornerRadius))
        // bottom right corner
        path.addQuadCurve(to: CGPoint(x: self.frame.size.width - cornerRadius, y: self.frame.size.height - shapeOffset), controlPoint: CGPoint(x: self.frame.size.width, y: self.frame.size.height - shapeOffset))

        // move to bottom center point before the triangle
        path.addLine(to: CGPoint(x: self.frame.size.width / 2 + triangleWidth / 2, y: self.frame.size.height - shapeOffset))
        
        // add triangle pointer
        path.addLine(to: CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height - shapeOffset + triangleHeight))
        path.addLine(to: CGPoint(x: self.frame.size.width / 2 - triangleWidth / 2, y: self.frame.size.height - shapeOffset))
        
//        // bottom left point before corner
//        path.addLine(to: CGPoint(x: cornerRadius, y: self.frame.size.height - shapeOffset))
//        // bottom left corner
        path.addQuadCurve(to: CGPoint(x: 0, y: self.frame.size.height - cornerRadius), controlPoint: CGPoint(x: 0, y: self.frame.size.height))

        path.close()

        shapeLayer.path = path.cgPath
    }
}
