//
//  AV_AddWaterVC.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/7/8.
//

import UIKit
import SVProgressHUD
import AppLovinSDK
import Alamofire
import FirebaseRemoteConfig
class AV_AddWaterVC: AV_RootVC {
    
    @IBOutlet weak var mcView: UIView!
    
    @IBOutlet weak var bgView: UIView!
    
    
    @IBOutlet weak var sureBtn: UIButton!
    
    @IBOutlet weak var mlTextf: UITextField!
    
    
    
    @IBOutlet weak var waterImageV: UIImageView!
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    
    @IBOutlet weak var cupuView: UIView!
    
    let cellWidth: CGFloat = 71
    let cellHeight: CGFloat = 71
    let cellSpacing: CGFloat = 12
    let dataimag = ["1","2", "3", "4", "5", "6","7","8"]
    let databigimag = ["1-1","2-2", "3-3", "4-4", "5-5", "6-6","7-7","8-8"]
    let drinkname = ["Water","Milk", "Tea", "Milky tea", "Beer", "Red wine","Fruit juice","Soup"]
    
    var rulerV = MLSliderRulerView()
    
    var customView: UIView!
    var bottomConstraint: NSLayoutConstraint?
    
    
    var retryAttempt = 0.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       
        setcollectionV()
        rulerV = MLSliderRulerView(frame:cupuView.bounds)

        self.cupuView.addSubview(rulerV)
        
        sureBtn.layer.cornerRadius = 25
        
        self.mcView.isHidden = true
        self.bgView.isHidden = true
        bgView.layer.cornerRadius = 30
    }
   
    func setcollectionV(){
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.collectionViewLayout = layout
        collectionView.register(UINib(nibName: "AV_AddWaterVCCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "AV_AddWaterVCCollectionViewCell")
    }
    
    
    @IBAction func back(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    @IBAction func addml(_ sender: UIButton) {
        let currentRulerValue = rulerV.rulerScrollView.currentRulerValue
                print("----\(currentRulerValue)")
        let defaults = UserDefaults.standard
        let answer = defaults.integer(forKey: "waveView")
        let neanswer = answer + Int(currentRulerValue)
        defaults.set(neanswer, forKey: "waveView")
        
        SVProgressHUD.showSuccess(withStatus: "Add drinking water successfully")
        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 1.5) {
            SVProgressHUD.dismiss()
        }
        
    }
    
    @IBAction func keyBoard(_ sender: Any) {
   
        LuxADManager.shareInstance().showADByRandomMode(callback: {
            actionType  in
            self.mcView.isHidden = false
            self.bgView.isHidden = false
            self.mlTextf.becomeFirstResponder()
        })

    }
    
    
    
    @IBAction func deleteBtn(_ sender: Any) {
        self.mcView.isHidden = true
        self.bgView.isHidden = true
        // 示例：关闭键盘
        mlTextf.resignFirstResponder()
    }
    
    
    @IBAction func sureBtn(_ sender: UIButton) {
        self.mcView.isHidden = true
        self.bgView.isHidden = true
        // 示例：关闭键盘
        mlTextf.resignFirstResponder()
        let defaults = UserDefaults.standard
        let answer = defaults.integer(forKey: "waveView")
        let neanswer = answer + (Int("\(mlTextf.text ?? "0")") ?? 0)
        defaults.set(neanswer, forKey: "waveView")
        navigationController?.popViewController(animated: true)
        SVProgressHUD.showSuccess(withStatus: "Add drinking water successfully")
        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 1.5) {
            SVProgressHUD.dismiss()
        }
    }
    
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
  
}
extension AV_AddWaterVC: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataimag.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AV_AddWaterVCCollectionViewCell", for: indexPath) as! AV_AddWaterVCCollectionViewCell
        cell.imageV.image = UIImage(named: "\(self.dataimag[indexPath.row])")
        cell.nameLabel.text = self.drinkname[indexPath.row]
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: cellWidth, height: cellHeight)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return cellSpacing
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: cellSpacing, bottom: 0, right: cellSpacing)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.waterImageV.image = UIImage(named: "\(indexPath.row + 1)-\(indexPath.row + 1)")
    }
    
}
