//
//  PhotoEditorEmojisVC.swift
//  WoWonder
//
//  Created by iMac on 23/05/25.
//  Copyright © 2025 ScriptSun. All rights reserved.
//

import UIKit

protocol PhotoEditorEmojisVCDelegate {
    func handleStickerSelection(data: String)
}

class PhotoEditorEmojisVC: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    // MARK: - Properties
    
    var parentContorller: PhotoEditingStickerToolVC!
    var emojisList: [String] = []
    var delegate: PhotoEditorEmojisVCDelegate?
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.setupCollectionView()
        self.setEmojiData()
    }
    
    static func emojisVC() -> PhotoEditorEmojisVC {
        let newVC = R.storyboard.photoEditor.photoEditorEmojisVC()
        return newVC!
    }
    
    // Setup Collection View
    func setupCollectionView() {
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.register(UINib(resource: R.nib.photoEditingToolEmojiCell), forCellWithReuseIdentifier: R.reuseIdentifier.photoEditingToolEmojiCell.identifier)
    }
    
    // Set Emoji Data
    func setEmojiData() {
        self.emojisList = [
            // <!-- Smiles -->
            "\u{1f604}",
            "\u{1f603}",
            "\u{1f600}",
            "\u{1f60a}",
            "\u{263a}",
            "\u{1f609}",
            "\u{1f60d}",
            "\u{1f618}",
            "\u{1f61a}",
            "\u{1f617}",
            "\u{1f619}",
            "\u{1f61c}",
            "\u{1f61d}",
            "\u{1f61b}",
            "\u{1f633}",
            "\u{1f601}",
            "\u{1f614}",
            "\u{1f60c}",
            "\u{1f612}",
            "\u{1f61e}",
            "\u{1f623}",
            "\u{1f622}",
            "\u{1f602}",
            "\u{1f62d}",
            "\u{1f62a}",
            "\u{1f625}",
            "\u{1f630}",
            "\u{1f605}",
            "\u{1f613}",
            "\u{1f629}",
            "\u{1f62b}",
            "\u{1f628}",
            "\u{1f631}",
            "\u{1f620}",
            "\u{1f621}",
            "\u{1f624}",
            
            // <!--People -->
            "\u{1f616}",
            "\u{1f606}",
            "\u{1f60b}",
            "\u{1f637}",
            "\u{1f60e}",
            "\u{1f634}",
            "\u{1f635}",
            "\u{1f632}",
            "\u{1f61f}",
            "\u{1f626}",
            "\u{1f627}",
            "\u{1f608}",
            "\u{1f47f}",
            "\u{1f62e}",
            "\u{1f62c}",
            "\u{1f610}",
            "\u{1f615}",
            "\u{1f62f}",
            "\u{1f636}",
            "\u{1f607}",
            "\u{1f60f}",
            "\u{1f611}",
            "\u{1f472}",
            "\u{1f473}",
            "\u{1f46e}",
            "\u{1f477}",
            "\u{1f482}",
            "\u{1f476}",
            "\u{1f466}",
            "\u{1f467}",
            "\u{1f468}",
            "\u{1f469}",
            "\u{1f474}",
            "\u{1f475}",
            "\u{1f471}",
            "\u{1f47c}",
            "\u{1f478}",
            "\u{1f63a}",
            "\u{1f638}",
            "\u{1f63b}",
            "\u{1f63d}",
            "\u{1f63c}",
            "\u{1f640}",
            "\u{1f63f}",
            "\u{1f639}",
            "\u{1f63e}",
            "\u{1f479}",
            "\u{1f47a}",
            "\u{1f648}",
            "\u{1f649}",
            "\u{1f64a}",
            "\u{1f480}",
            "\u{1f47d}",
            "\u{1f4a9}",
            "\u{1f525}",
            "\u{2728}",
            "\u{1f31f}",
            "\u{1f4ab}",
            "\u{1f4a5}",
            "\u{1f4a2}",
            "\u{1f4a6}",
            "\u{1f4a7}",
            "\u{1f4a4}",
            "\u{1f4a8}",
            "\u{1f442}",
            "\u{1f440}",
            "\u{1f443}",
            "\u{1f445}",
            "\u{1f444}",
            "\u{1f44d}",
            "\u{1f44e}",
            "\u{1f44c}",
            "\u{1f44a}",
            "\u{270a}",
            "\u{270c}",
            "\u{1f44b}",
            "\u{270b}",
            "\u{1f450}",
            "\u{1f446}",
            "\u{1f447}",
            "\u{1f449}",
            "\u{1f448}",
            "\u{1f64c}",
            "\u{1f64f}",
            "\u{261d}",
            "\u{1f44f}",
            "\u{1f4aa}",
            "\u{1f6b6}",
            "\u{1f3c3}",
            "\u{1f483}",
            "\u{1f46b}",
            "\u{1f46a}",
            "\u{1f46c}",
            "\u{1f46d}",
            "\u{1f48f}",
            "\u{1f491}",
            "\u{1f46f}",
            "\u{1f646}",
            "\u{1f645}",
            "\u{1f481}",
            "\u{1f64b}",
            "\u{1f486}",
            "\u{1f487}",
            "\u{1f485}",
            "\u{1f470}",
            "\u{1f64e}",
            "\u{1f64d}",
            "\u{1f647}",
            "\u{1f3a9}",
            "\u{1f451}",
            "\u{1f452}",
            "\u{1f45f}",
            "\u{1f45e}",
            "\u{1f461}",
            "\u{1f460}",
            "\u{1f462}",
            "\u{1f455}",
            "\u{1f454}",
            "\u{1f45a}",
            "\u{1f457}",
            "\u{1f3bd}",
            "\u{1f456}",
            "\u{1f458}",
            "\u{1f459}",
            "\u{1f4bc}",
            "\u{1f45c}",
            "\u{1f45d}",
            "\u{1f45b}",
            "\u{1f453}",
            "\u{1f380}",
            "\u{1f302}",
            "\u{1f484}",
            "\u{1f49b}",
            "\u{1f499}",
            "\u{1f49c}",
            "\u{1f49a}",
            "\u{2764}",
            "\u{1f494}",
            "\u{1f497}",
            "\u{1f493}",
            "\u{1f495}",
            "\u{1f496}",
            "\u{1f49e}",
            "\u{1f498}",
            "\u{1f48c}",
            "\u{1f48b}",
            "\u{1f48d}",
            "\u{1f48e}",
            "\u{1f464}",
            "\u{1f465}",
            "\u{1f4ac}",
            "\u{1f463}",
            "\u{1f4ad}",
            
            // <!--  Nature-->
            "\u{1f436}",
            "\u{1f43a}",
            "\u{1f431}",
            "\u{1f42d}",
            "\u{1f439}",
            "\u{1f430}",
            "\u{1f438}",
            "\u{1f42f}",
            "\u{1f428}",
            "\u{1f43b}",
            "\u{1f437}",
            "\u{1f43d}",
            "\u{1f42e}",
            "\u{1f417}",
            "\u{1f435}",
            "\u{1f412}",
            "\u{1f434}",
            "\u{1f411}",
            "\u{1f418}",
            "\u{1f43c}",
            "\u{1f427}",
            "\u{1f426}",
            "\u{1f424}",
            "\u{1f425}",
            "\u{1f423}",
            "\u{1f414}",
            "\u{1f40d}",
            "\u{1f422}",
            "\u{1f41b}",
            "\u{1f41d}",
            "\u{1f41c}",
            "\u{1f41e}",
            "\u{1f40c}",
            "\u{1f419}",
            "\u{1f41a}",
            "\u{1f420}",
            "\u{1f41f}",
            "\u{1f42c}",
            "\u{1f433}",
            "\u{1f40b}",
            "\u{1f404}",
            "\u{1f40f}",
            "\u{1f400}",
            "\u{1f403}",
            "\u{1f405}",
            "\u{1f407}",
            "\u{1f409}",
            "\u{1f40e}",
            "\u{1f410}",
            "\u{1f413}",
            "\u{1f415}",
            "\u{1f416}",
            "\u{1f401}",
            "\u{1f402}",
            "\u{1f432}",
            "\u{1f421}",
            "\u{1f40a}",
            "\u{1f42b}",
            "\u{1f42a}",
            "\u{1f406}",
            "\u{1f408}",
            "\u{1f429}",
            "\u{1f43e}",
            "\u{1f490}",
            "\u{1f338}",
            "\u{1f337}",
            "\u{1f340}",
            "\u{1f339}",
            "\u{1f33b}",
            "\u{1f33a}",
            "\u{1f341}",
            "\u{1f343}",
            "\u{1f342}",
            "\u{1f33f}",
            "\u{1f33e}",
            "\u{1f344}",
            "\u{1f335}",
            "\u{1f334}",
            "\u{1f332}",
            "\u{1f333}",
            "\u{1f330}",
            "\u{1f331}",
            "\u{1f33c}",
            "\u{1f310}",
            "\u{1f31e}",
            "\u{1f31d}",
            "\u{1f31a}",
            "\u{1f311}",
            "\u{1f312}",
            "\u{1f313}",
            "\u{1f314}",
            "\u{1f315}",
            "\u{1f316}",
            "\u{1f317}",
            "\u{1f318}",
            "\u{1f31c}",
            "\u{1f31b}",
            "\u{1f319}",
            "\u{1f30d}",
            "\u{1f30e}",
            "\u{1f30f}",
            "\u{1f30b}",
            "\u{1f30c}",
            "\u{1f320}",
            "\u{2b50}",
            "\u{2600}",
            "\u{26c5}",
            "\u{2601}",
            "\u{26a1}",
            "\u{2614}",
            "\u{2744}",
            "\u{26c4}",
            "\u{1f300}",
            "\u{1f301}",
            "\u{1f308}",
            "\u{1f30a}",
            
            // <!--Places-->
            "\u{1f3e0}",
            "\u{1f3e1}",
            "\u{1f3eb}",
            "\u{1f3e2}",
            "\u{1f3e3}",
            "\u{1f3e5}",
            "\u{1f3e6}",
            "\u{1f3ea}",
            "\u{1f3e9}",
            "\u{1f3e8}",
            "\u{1f492}",
            "\u{26ea}",
            "\u{1f3ec}",
            "\u{1f3e4}",
            "\u{1f307}",
            "\u{1f306}",
            "\u{1f3ef}",
            "\u{1f3f0}",
            "\u{26fa}",
            "\u{1f3ed}",
            "\u{1f5fc}",
            "\u{1f5fe}",
            "\u{1f5fb}",
            "\u{1f304}",
            "\u{1f305}",
            "\u{1f303}",
            "\u{1f5fd}",
            "\u{1f309}",
            "\u{1f3a0}",
            "\u{1f3a1}",
            "\u{26f2}",
            "\u{1f3a2}",
            "\u{1f6a2}",
            "\u{26f5}",
            "\u{1f6a4}",
            "\u{1f6a3}",
            "\u{2693}",
            "\u{1f680}",
            "\u{2708}",
            "\u{1f4ba}",
            "\u{1f681}",
            "\u{1f682}",
            "\u{1f68a}",
            "\u{1f689}",
            "\u{1f69e}",
            "\u{1f686}",
            "\u{1f684}",
            "\u{1f685}",
            "\u{1f688}",
            "\u{1f687}",
            "\u{1f69d}",
            "\u{1f68b}",
            "\u{1f683}",
            "\u{1f68e}",
            "\u{1f68c}",
            "\u{1f68d}",
            "\u{1f699}",
            "\u{1f698}",
            "\u{1f697}",
            "\u{1f695}",
            "\u{1f696}",
            "\u{1f69b}",
            "\u{1f69a}",
            "\u{1f6a8}",
            "\u{1f693}",
            "\u{1f694}",
            "\u{1f692}",
            "\u{1f691}",
            "\u{1f690}",
            "\u{1f6b2}",
            "\u{1f6a1}",
            "\u{1f69f}",
            "\u{1f6a0}",
            "\u{1f69c}",
            "\u{1f488}",
            "\u{1f68f}",
            "\u{1f3ab}",
            "\u{1f6a6}",
            "\u{1f6a5}",
            "\u{26a0}",
            "\u{1f6a7}",
            "\u{1f530}",
            "\u{26fd}",
            "\u{1f3ee}",
            "\u{1f3b0}",
            "\u{2668}",
            "\u{1f5ff}",
            "\u{1f3aa}",
            "\u{1f3ad}",
            "\u{1f4cd}",
            "\u{1f6a9}"
        ]
        self.collectionView.reloadData()
    }
}

// MARK: Collection View Setup
extension PhotoEditorEmojisVC: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch collectionView {
        case self.collectionView:
            return self.emojisList.count
        default:
            return 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch collectionView {
        case self.collectionView:
            let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.photoEditingToolEmojiCell.identifier, for: indexPath) as! PhotoEditingToolEmojiCell
            let item = self.emojisList[indexPath.item]
            cell.setData(data: item)
            return cell
        default:
            return UICollectionViewCell()
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch collectionView {
        case self.collectionView:
            let item = self.emojisList[indexPath.item]
            self.delegate?.handleStickerSelection(data: item)
        default:
            break
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
}
