//
//  ColorEditorColorToolVC.swift
//  WoWonder
//
//  Created by iMac on 04/06/25.
//  Copyright © 2025 ScriptSun. All rights reserved.
//

import UIKit
import FittedSheets

struct GradientColorModel {
    let id: Int
    let colors: [CGColor]
}

protocol ColorEditorColorToolVCDelegate {
    func handleColorToolSelectedColor(color: GradientColorModel)
    func handleColorToolCloseButtonTap()
    func handleColorToolSaveButtonTap(color: GradientColorModel)
}

class ColorEditorColorToolVC: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var colorCollectionView: UICollectionView!
    
    // MARK: - Properties
    
    private var colorList: [GradientColorModel] = []
    var selectedColor = GradientColorModel(id: 23, colors: [UIColor(hex: "#fc8f35").cgColor, UIColor(hex: "#de2362").cgColor])
    var delegate: ColorEditorColorToolVCDelegate?
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Close Button Action
    @IBAction func closeButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.sheetViewController?.attemptDismiss(animated: true)
        self.delegate?.handleColorToolCloseButtonTap()
    }
    
    // Save Button Action
    @IBAction func saveButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.sheetViewController?.attemptDismiss(animated: true)
        self.delegate?.handleColorToolSaveButtonTap(color: self.selectedColor)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.setupCollectionView()
        self.setBrushColorData()
    }
    
    // Setup Collection View
    func setupCollectionView() {
        self.colorCollectionView.delegate = self
        self.colorCollectionView.dataSource = self
        self.colorCollectionView.register(UINib(resource: R.nib.photoEditingToolColorPickerCell), forCellWithReuseIdentifier: R.reuseIdentifier.photoEditingToolColorPickerCell.identifier)
    }
    
    // Set Brush Color Data
    func setBrushColorData() {
        self.colorList = [
            GradientColorModel(id: 23, colors: [UIColor(hex:"#fc8f35").cgColor, UIColor(hex:"#de2362").cgColor]),
            GradientColorModel(id: 24, colors: [UIColor(hex:"#78c2eb").cgColor, UIColor(hex:"#3c9fc7").cgColor]),
            GradientColorModel(id: 25, colors: [UIColor(hex:"#2393cf").cgColor, UIColor(hex:"#b02071").cgColor]),
            GradientColorModel(id: 26, colors: [UIColor(hex:"#3dccd1").cgColor, UIColor(hex:"#dec53a").cgColor]),
            GradientColorModel(id: 27, colors: [UIColor(hex:"#ed6fb7").cgColor, UIColor(hex:"#e02d7e").cgColor]),
            GradientColorModel(id: 28, colors: [UIColor(hex:"#55bec2").cgColor, UIColor(hex:"#d6866f").cgColor]),
            GradientColorModel(id: 29, colors: [UIColor(hex:"#09479e").cgColor, UIColor(hex:"#28aac7").cgColor]),
            GradientColorModel(id: 30, colors: [UIColor(hex:"#db1f61").cgColor, UIColor(hex:"#fa5732").cgColor]),
            GradientColorModel(id: 31, colors: [UIColor(hex:"#83a4e6").cgColor, UIColor(hex:"#e82cb0").cgColor]),
            GradientColorModel(id: 32, colors: [UIColor(hex:"#6674b3").cgColor, UIColor(hex:"#e86033").cgColor]),
            GradientColorModel(id: 33, colors: [UIColor(hex:"#06c749").cgColor, UIColor(hex:"#431e87").cgColor]),
            GradientColorModel(id: 34, colors: [UIColor(hex:"#ff6b54").cgColor, UIColor(hex:"#fa1007").cgColor]),
            GradientColorModel(id: 35, colors: [UIColor(hex:"#030303").cgColor, UIColor(hex:"#5a5c5b").cgColor]),
            GradientColorModel(id: 36, colors: [UIColor(hex:"#2a3238").cgColor, UIColor(hex:"#7c848a").cgColor]),
            GradientColorModel(id: 37, colors: [UIColor(hex:"#c2c7cc").cgColor, UIColor(hex:"#9ea4b0").cgColor]),
            GradientColorModel(id: 38, colors: [UIColor(hex:"#ef5350").cgColor, UIColor(hex:"#b71c1c").cgColor]),
            GradientColorModel(id: 39, colors: [UIColor(hex:"#EC407A").cgColor, UIColor(hex:"#880E4F").cgColor]),
            GradientColorModel(id: 40, colors: [UIColor(hex:"#AB47BC").cgColor, UIColor(hex:"#4A148C").cgColor]),
            GradientColorModel(id: 41, colors: [UIColor(hex:"#5C6BC0").cgColor, UIColor(hex:"#1A237E").cgColor]),
            GradientColorModel(id: 42, colors: [UIColor(hex:"#29B6F6").cgColor, UIColor(hex:"#01579B").cgColor]),
            GradientColorModel(id: 43, colors: [UIColor(hex:"#26A69A").cgColor, UIColor(hex:"#004D40").cgColor]),
            GradientColorModel(id: 44, colors: [UIColor(hex:"#9CCC65").cgColor, UIColor(hex:"#33691E").cgColor]),
            GradientColorModel(id: 45, colors: [UIColor(hex:"#FFEE58").cgColor, UIColor(hex:"#F57F17").cgColor]),
            GradientColorModel(id: 46, colors: [UIColor(hex:"#FF7043").cgColor, UIColor(hex:"#BF360C").cgColor]),
            GradientColorModel(id: 47, colors: [UIColor(hex:"#BDBDBD").cgColor, UIColor(hex:"#424242").cgColor]),
            GradientColorModel(id: 48, colors: [UIColor(hex:"#78909C").cgColor, UIColor(hex:"#263238").cgColor]),
            GradientColorModel(id: 49, colors: [UIColor(hex:"#6ec052").cgColor, UIColor(hex:"#28c4f3").cgColor]),
            GradientColorModel(id: 50, colors: [UIColor(hex:"#fcca5b").cgColor, UIColor(hex:"#ed4955").cgColor]),
            GradientColorModel(id: 51, colors: [UIColor(hex:"#3033c6").cgColor, UIColor(hex:"#fb0049").cgColor])
        ]
        self.colorCollectionView.reloadData()
    }
    
}

// MARK: - Extension

// MARK: Collection View Setup
extension ColorEditorColorToolVC: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch collectionView {
        case self.colorCollectionView:
            return self.colorList.count
        default:
            return 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch collectionView {
        case self.colorCollectionView:
            let cell = self.colorCollectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.photoEditingToolColorPickerCell.identifier, for: indexPath) as! PhotoEditingToolColorPickerCell
            let item = self.colorList[indexPath.item]
            cell.setData(data: item)
            if selectedColor.id == item.id {
                cell.colorView.layer.borderColor = UIColor.accent.cgColor
            } else {
                cell.colorView.layer.borderColor = UIColor.clear.cgColor
            }
            return cell
        default:
            return UICollectionViewCell()
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch collectionView {
        case self.colorCollectionView:
            let item = self.colorList[indexPath.item]
            self.selectedColor = item
            self.colorCollectionView.reloadData()
            self.delegate?.handleColorToolSelectedColor(color: 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
    }
    
}
