//
//  ViewController.swift
//  DevourAndDrought
//
//  Created by Micah Gomez on 10/17/21.
//

import UIKit

class PlayerBoardController: UIViewController {
	
	@IBOutlet var playerNameLabel: UILabel!
	
	@IBOutlet var playerOilCount: UILabel!
	@IBOutlet var playerWaterCount: UILabel!
	@IBOutlet var playerWheatCount: UILabel!
	@IBOutlet var playerCornCount: UILabel!
	@IBOutlet var playerFishCount: UILabel!
	@IBOutlet var playerCattleCount: UILabel!
	@IBOutlet var medallionCount: UILabel!
	

	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view.
		
	}
	
	override func viewWillAppear(_ animated: Bool) {
		
		playerNameLabel.text = GlobalVars.currentPlayer.name
		playerOilCount.text = String("")
		playerWaterCount.text = String("")
		playerWheatCount.text = String("")
		playerCornCount.text = String("")
		playerFishCount.text = String("")
		playerCattleCount.text = String("")
		medallionCount.text = String("")

		for _ in 0...GlobalVars.currentPlayer.oil {
			playerOilCount.text?.append("🪔")
		}
		for _ in 0...GlobalVars.currentPlayer.water {
			playerWaterCount.text?.append("💧")
		}
		for _ in 0...GlobalVars.currentPlayer.wheat {
			playerWheatCount.text?.append("🌾")
		}
		for _ in 0...GlobalVars.currentPlayer.corn {
			playerCornCount.text?.append("🌽")
		}
		for _ in 0...GlobalVars.currentPlayer.fish {
			playerFishCount.text?.append("🐟")
		}
		for _ in 0...GlobalVars.currentPlayer.cattle {
			playerCattleCount.text?.append("🐂")
		}
		
		playerOilCount.text?.remove(at: (playerOilCount.text?.firstIndex(of: "🪔"))!)
		playerWaterCount.text?.remove(at: (playerWaterCount.text?.firstIndex(of: "💧"))!)
		playerWheatCount.text?.remove(at: (playerWheatCount.text?.firstIndex(of: "🌾"))!)
		playerCornCount.text?.remove(at: (playerCornCount.text?.firstIndex(of: "🌽"))!)
		playerFishCount.text?.remove(at: (playerFishCount.text?.firstIndex(of: "🐟"))!)
		playerCattleCount.text?.remove(at: (playerCattleCount.text?.firstIndex(of: "🐂"))!)
		
		if(GlobalVars.currentPlayer.oil >= 10){
			playerOilCount.text?.append("☀️")
		}
		if(GlobalVars.currentPlayer.water >= 10){
			   playerWaterCount.text?.append("☀️")
		}
		if(GlobalVars.currentPlayer.wheat >= 10){
			playerWheatCount.text?.append("☀️")
		}
		if(GlobalVars.currentPlayer.corn >= 10){
			   playerCornCount.text?.append("☀️")
		}
		if(GlobalVars.currentPlayer.fish >= 10){
			   playerFishCount.text?.append("☀️")
		}
		if(GlobalVars.currentPlayer.cattle >= 10){
			   playerCattleCount.text?.append("☀️")
		}
		
		for i in 0...9 {
			if ((GlobalVars.currentPlayer.oil >= i)&&(GlobalVars.currentPlayer.water >= i)&&(GlobalVars.currentPlayer.wheat >= i)&&(GlobalVars.currentPlayer.corn >= i)&&(GlobalVars.currentPlayer.fish >= i)&&(GlobalVars.currentPlayer.cattle >= i)){
				medallionCount.text?.append("☀️")
			}
		}
		medallionCount.text?.remove(at: (medallionCount.text?.firstIndex(of: "☀️"))!)
		
	}

	@IBAction func EndTurn(_ sender: Any) {
	
		print("year: \(GlobalVars.currentYear)")
		print("playerCount: \(GlobalVars.playerCount)")
		print("activePlayer: \(GlobalVars.activePlayerInt)")
		
		GlobalVars.players[GlobalVars.activePlayerInt] = GlobalVars.currentPlayer
		
		if((GlobalVars.currentYear == 6) && ((GlobalVars.activePlayerInt+1) == GlobalVars.playerCount)){
			let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
			if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "GameEndScreen") as? UIViewController {
				self.present(viewController, animated: true, completion: nil)
			}
		} else {
		   
			let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
			if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "PlayerStart") as? UIViewController {
				self.present(viewController, animated: true, completion: nil)
		   }
		}
		
		
	}
	

}

class GameBoardController: UIViewController {
	
	@IBOutlet var PlayerNameLabel: UILabel!
	@IBOutlet var LbrPointsLbl: UILabel!
	
	@IBOutlet var ResourceSlot1: UIButton!
	@IBOutlet var ResourceSlot2: UIButton!
	@IBOutlet var ResourceSlot3: UIButton!
	@IBOutlet var ResourceSlot4: UIButton!
	@IBOutlet var ResourceSlot5: UIButton!
	@IBOutlet var ResourceSlot6: UIButton!
	@IBOutlet var ResourceSlot7: UIButton!
	
	@IBOutlet var AllResourceSlots : [UIButton]!
	
	//let buttonList:[UIButton] = [ResourceSlot1,ResourceSlot2,ResourceSlot3,ResourceSlot4,ResourceSlot5,ResourceSlot6,ResourceSlot7]
	
	var resourceList:[resource] = []
	//var resourceSlots:[resource] = []
	

	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view.
		
		let resourceNames:[String] = ["Oil\n🪔","Water\n💧","Wheat\n🌾","Corn\n🌽","Fish\n🐟","Cattle\n🐂"]
		let resourceCosts:[Int] = [1,2,3,4,5,6]
		
		for i in 0...5 {
			let createResource = resource()
			createResource.name = resourceNames[i]
			createResource.cost = resourceCosts[i]
			resourceList.append(createResource)
		}
		
		
		if (GlobalVars.resourceSlots.count == 0){
			for y in 0...6 {
				let createSlot = resourceList.randomElement()
				print(createSlot?.name)
				print(createSlot?.cost)
				/*if(y == GlobalVars.currentYear){
					createSlot?.cost = createSlot!.cost - 1
				}*/
				GlobalVars.resourceSlots.append(createSlot!)
			}
		}
		
		//MARK: TODO: add cost after discount has been applied
		for (index,item) in AllResourceSlots.enumerated() {
			print(index,item)
			//item.setTitle("\(GlobalVars.resourceSlots[index].name)", for: .normal)
			//item.setTitle("\(GlobalVars.resourceSlots[index].name)\n/*\(GlobalVars.resourceSlots[index].cost)", for: .normal)
			item.titleLabel?.numberOfLines = 0
			item.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
			item.titleLabel?.textAlignment = NSTextAlignment.center
			//item.layer.shadowPath = UIBezierPath(rect: item.layer.bounds).cgPath
			item.layer.shadowColor = UIColor.black.cgColor
			item.layer.shadowRadius = 13.0
			item.layer.shadowOpacity = 0
			if(GlobalVars.currentYear == index){
				item.layer.borderColor = UIColor.green.cgColor
				item.layer.borderWidth = 5
			}
			if(GlobalVars.resourceSlots[GlobalVars.currentYear].name == GlobalVars.resourceSlots[index].name){
				item.setTitle("\(GlobalVars.resourceSlots[index].name)\n\((GlobalVars.resourceSlots[index].cost - 1))", for: .normal)
			} else {
				item.setTitle("\(GlobalVars.resourceSlots[index].name)\n\(GlobalVars.resourceSlots[index].cost)", for: .normal)
			}
		}
	}
	
	override func viewWillAppear(_ animated: Bool) {
		PlayerNameLabel.text = GlobalVars.players[GlobalVars.activePlayerInt].name
		LbrPointsLbl.text = "Labor Points: \(GlobalVars.currentPlayer.laborpoints)"
	}
/*
	@IBAction func ResourceSlotOne(_ sender: Any) {
		ResourceSlot1.setTitle("\(resourceSlots[0].name)\n\(resourceSlots[0].cost)", for: .normal)
	}
	
	@IBAction func ResourceSlotTwo(_ sender: Any) {
		ResourceSlot2.setTitle("\(resourceSlots[1].name)\n\(resourceSlots[1].cost)", for: .normal)
	}
	
	@IBAction func ResourceSlotThree(_ sender: Any) {
		ResourceSlot3.setTitle("\(resourceSlots[2].name)\n\(resourceSlots[2].cost)", for: .normal)
	}
	
	@IBAction func ResourceSlotFour(_ sender: Any) {
		ResourceSlot4.setTitle("\(resourceSlots[3].name)\n\(resourceSlots[3].cost)", for: .normal)
	}
	
	@IBAction func ResourceSlotFive(_ sender: Any) {
		ResourceSlot5.setTitle("\(resourceSlots[4].name)\n\(resourceSlots[4].cost)", for: .normal)
	}
	
	@IBAction func ResourceSlotSix(_ sender: Any) {
		ResourceSlot6.setTitle("\(resourceSlots[5].name)\n\(resourceSlots[5].cost)", for: .normal)
	}
	
	@IBAction func ResourceSlotSeven(_ sender: UIButton) {
		ResourceSlot7.setTitle("\(resourceSlots[6].name)\n\(resourceSlots[6].cost)", for: .normal)
	}
	*/
	
	@IBAction func boxTouched(_ sender: UIButton) {
		let index = AllResourceSlots.index(of: sender)!
		if !sender.isSelected{
			for (i, item) in GlobalVars.resourceSlots.enumerated() {
				//print(i,item)
				
				AllResourceSlots[i].isSelected = false
				AllResourceSlots[i].layer.shadowOpacity = 0
				if item.name == GlobalVars.resourceSlots[index].name {
					AllResourceSlots[i].isSelected = true
					AllResourceSlots[i].layer.shadowOpacity = 0.75
				}
			}
		} else {
			if(AllResourceSlots[GlobalVars.currentYear].isSelected){
				if((GlobalVars.currentPlayer.laborpoints - (GlobalVars.resourceSlots[index].cost - 1)) >= 0){
					GlobalVars.currentPlayer.laborpoints = GlobalVars.currentPlayer.laborpoints - (GlobalVars.resourceSlots[index].cost - 1)
				} else {
					return
				}
			} else {
				if((GlobalVars.currentPlayer.laborpoints - GlobalVars.resourceSlots[index].cost) >= 0){
					GlobalVars.currentPlayer.laborpoints = GlobalVars.currentPlayer.laborpoints - GlobalVars.resourceSlots[index].cost
				} else {
					return
				}
				
			}
			
			
			print("PURCHASED!!!")
			for (i, item) in GlobalVars.resourceSlots.enumerated() {
				if AllResourceSlots[i].isSelected {
					addToInventory(name: GlobalVars.resourceSlots[i].name)
					print("resource name: \(GlobalVars.resourceSlots[i].name)")
					GlobalVars.resourceSlots[i] = resourceList.randomElement()!
					/*AllResourceSlots[i].setTitle("\(GlobalVars.resourceSlots[i].name)\n\(GlobalVars.resourceSlots[i].cost)", for: .normal)
					if(GlobalVars.resourceSlots[GlobalVars.currentYear].name == GlobalVars.resourceSlots[i].name){
						AllResourceSlots[i].setTitle("\(GlobalVars.resourceSlots[i].name)\n\((GlobalVars.resourceSlots[i].cost - 1))", for: .normal)
					} else {
						AllResourceSlots[i].setTitle("\(GlobalVars.resourceSlots[i].name)\n\(GlobalVars.resourceSlots[i].cost)", for: .normal)
					}*/
					for (index,ii) in AllResourceSlots.enumerated() {
						if(GlobalVars.resourceSlots[GlobalVars.currentYear].name == GlobalVars.resourceSlots[index].name){
							ii.setTitle("\(GlobalVars.resourceSlots[index].name)\n\((GlobalVars.resourceSlots[index].cost - 1))", for: .normal)
						} else {
							ii.setTitle("\(GlobalVars.resourceSlots[index].name)\n\(GlobalVars.resourceSlots[index].cost)", for: .normal)
						}
					}
				}
				AllResourceSlots[i].isSelected = false
				AllResourceSlots[i].layer.shadowOpacity = 0
				LbrPointsLbl.text = "Labor Points: \(GlobalVars.currentPlayer.laborpoints)"
			}
		}
		print(GlobalVars.currentPlayer.oil,GlobalVars.currentPlayer.water,GlobalVars.currentPlayer.wheat,GlobalVars.currentPlayer.corn,GlobalVars.currentPlayer.fish,GlobalVars.currentPlayer.cattle)
		
	}
	
	//MARK: TODO: DONT ALLOW PURCHASE IF NOT ENOUGH LABOR POINTS
	func addToInventory(name: String){
		print("adding!")
		switch(name) {
			case "Oil\n🪔" :
				GlobalVars.currentPlayer.oil = GlobalVars.currentPlayer.oil + 1
				if(GlobalVars.currentPlayer.oil > 10){
					GlobalVars.currentPlayer.oil = 10
				}
				break;
			case "Water\n💧" :
				GlobalVars.currentPlayer.water = GlobalVars.currentPlayer.water + 1
				if(GlobalVars.currentPlayer.water > 10){
					GlobalVars.currentPlayer.water = 10
				}
				break;
			case "Wheat\n🌾" :
				GlobalVars.currentPlayer.wheat = GlobalVars.currentPlayer.wheat + 1
				if(GlobalVars.currentPlayer.wheat > 10){
					GlobalVars.currentPlayer.wheat = 10
				}
				break;
			case "Corn\n🌽" :
				GlobalVars.currentPlayer.corn = GlobalVars.currentPlayer.corn + 1
				if(GlobalVars.currentPlayer.corn > 10){
					GlobalVars.currentPlayer.corn = 10
				}
				break;
			case "Fish\n🐟" :
				GlobalVars.currentPlayer.fish = GlobalVars.currentPlayer.fish + 1
				if(GlobalVars.currentPlayer.fish > 10){
					GlobalVars.currentPlayer.fish = 10
				}
				break;
			case "Cattle\n🐂" :
				GlobalVars.currentPlayer.cattle = GlobalVars.currentPlayer.cattle + 1
				if(GlobalVars.currentPlayer.cattle > 10){
					GlobalVars.currentPlayer.cattle = 10
				}
				break;
		  
		   /* you can have any number of case statements */
		   default : /* Optional */
			  print("uh oh!")
		}
	}
	
}
