package com.gogame.minesweeper

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.gogame.minesweeper.bean.GameState
import com.gogame.minesweeper.databinding.ActivityMainBinding
import com.gogame.minesweeper.swipe.SwipeActivity
import com.google.gson.Gson

class MainActivity : BaseActivity() {

    private lateinit var binding: ActivityMainBinding

    private lateinit var difficultyLevels: Array<DifficultyLevel>
    private var currentDifficultyIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        immersionBar()
        initView()
    }

    private fun immersionBar() {
        //自适应沉浸式
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
        windowInsetsController.isAppearanceLightStatusBars = true//状态栏文字颜色
    }

    private fun initView() {
        // 设置难度级别
        difficultyLevels = DifficultyLevel.entries.toTypedArray() // 获取所有的难度级别

        // 显示初始难度
        binding.tvDifficulty.text = difficultyLevels[currentDifficultyIndex].name

        binding.btnPrevious.setOnClickListener {
            if (currentDifficultyIndex > 0) {
                currentDifficultyIndex--
                binding.tvDifficulty.text = difficultyLevels[currentDifficultyIndex].name
            }
            updateContinueState()
        }

        binding.btnNext.setOnClickListener {
            if (currentDifficultyIndex < difficultyLevels.size - 1) {
                currentDifficultyIndex++
                binding.tvDifficulty.text = difficultyLevels[currentDifficultyIndex].name
            }
            updateContinueState()
        }

        binding.newGameBtn.setOnClickListener {
            startGameWithDifficulty()
        }
        binding.continueBtn.setOnClickListener {
            continuePreviousGame()
        }
    }

    override fun onResume() {
        super.onResume()
        updateContinueState()
    }

    private fun startGameWithDifficulty() {
        val intent = Intent(this, SwipeActivity::class.java)
        // 传递当前难度给 SwipeActivity
        intent.putExtra(
            "difficulty",
            difficultyLevels[currentDifficultyIndex]
        ) // 直接传递 DifficultyLevel
        intent.putExtra("isNewGame", true)
        startActivity(intent)
    }

    private fun continuePreviousGame() {
        val intent = Intent(this, SwipeActivity::class.java)
        // 传递当前难度给 SwipeActivity
        intent.putExtra(
            "difficulty",
            difficultyLevels[currentDifficultyIndex]
        ) // 直接传递 DifficultyLevel
        intent.putExtra("isNewGame", false)
        startActivity(intent)
    }

    private fun updateContinueState() {
        val sharedPreferences = getSharedPreferences("minesweeper_prefs", Context.MODE_PRIVATE)
        val gameStateJson = sharedPreferences.getString("game_state", null)
        if (gameStateJson.isNullOrEmpty()) {
            binding.continueBtn.visibility = View.GONE
            return
        }
        val gameState: GameState = Gson().fromJson(gameStateJson, GameState::class.java)
        if (gameState.difficulty == difficultyLevels[currentDifficultyIndex]) {
            binding.continueBtn.visibility = View.VISIBLE
        } else {
            binding.continueBtn.visibility = View.GONE
        }
    }
}
