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

import UIKit

enum StickerType {
    case sticker
    case emoji
    case widget
}

struct PhotoStickerModel {
    let url: String
    let type: StickerType
}

protocol PhotoEditorStickerVCDelegate {
    func handleStickerSelection(data: PhotoStickerModel)
}

class PhotoEditorStickerVC: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    // MARK: - Properties
    
    var parentContorller: PhotoEditingStickerToolVC!
    var stickerList: [PhotoStickerModel] = []
    var delegate: PhotoEditorStickerVCDelegate?
    
    // 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.setStickerData()
    }
    
    static func stickerVC() -> PhotoEditorStickerVC {
        let newVC = R.storyboard.photoEditor.photoEditorStickerVC()
        return newVC!
    }
    
    // Setup Collection View
    func setupCollectionView() {
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.register(UINib(resource: R.nib.photoEditingToolStickerCell), forCellWithReuseIdentifier: R.reuseIdentifier.photoEditingToolStickerCell.identifier)
    }
    
    // Set Sticker Data
    func setStickerData() {
        self.stickerList = [
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/001.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/002.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/003.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/004.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/005.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/006.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/007.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/008.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/009.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/010.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/011.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/012.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/013.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/014.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/015.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/016.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/017.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/018.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/019.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/020.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/021.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/022.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/023.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/024.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/025.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/026.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/027.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/028.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/029.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/030.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/031.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/032.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/033.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/034.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/035.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/036.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/037.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/038.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/5c1379e1040ab12ebe4b5697/iPhone/039.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/001.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/002.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/003.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/004.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/005.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/006.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/007.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/008.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/009.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/010.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/011.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/012.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/013.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/014.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/015.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/016.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/017.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/018.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/019.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/020.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/021.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/022.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/023.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/024.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/025.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/026.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/027.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/028.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/029.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/030.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/031.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/032.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/033.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/034.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/035.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/036.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/037.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/038.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/sticonshop/v1/sticon/6620d82cf52df955bb610ddf/iPhone/039.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688558/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688559/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688562/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688563/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688564/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688568/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688570/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688571/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688578/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688575/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688576/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/68688580/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/22108/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/22112/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/22111/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/13456187/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/13456184/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/13456186/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/13456188/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/13456182/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/23944390/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/23944368/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135256/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135257/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135258/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135259/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135260/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135261/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135262/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135263/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135264/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135265/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135266/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135267/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/sticker/29135268/ANDROID/sticker.png", type: .sticker),
            PhotoStickerModel(url: "https://stickershop.line-scdn.net/stickershop/v1/product/1041829/LINEStorePC/main.png", type: .sticker),
        ]
        self.collectionView.reloadData()
    }
    
}

// MARK: Collection View Setup
extension PhotoEditorStickerVC: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch collectionView {
        case self.collectionView:
            return self.stickerList.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.photoEditingToolStickerCell.identifier, for: indexPath) as! PhotoEditingToolStickerCell
            let item = self.stickerList[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.stickerList[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
    }
    
}
