package com.example.shiftalarm

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Build
import android.view.LayoutInflater
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.FileProvider
import org.json.JSONObject
import java.io.BufferedInputStream
import java.io.File
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.net.URL

object AppUpdateManager {

    private const val VERSION_URL = "https://git.webpluss.net/sanjeok77/ShiftRing/raw/branch/main/version.json"

    fun checkUpdate(activity: Activity, silent: Boolean = false) {
        val ctx = activity.applicationContext
        val versionCheckUrl = "$VERSION_URL?t=${System.currentTimeMillis()}"

        Thread {
            try {
                val url = URL(versionCheckUrl)
                val connection = url.openConnection() as HttpURLConnection
                connection.connectTimeout = 5000
                connection.readTimeout = 5000
                connection.requestMethod = "GET"
                connection.useCaches = false

                if (connection.responseCode == 200) {
                    val reader = connection.inputStream.bufferedReader()
                    val result = reader.readText()
                    reader.close()

                    val json = JSONObject(result)
                    val serverVersionCode = json.getInt("versionCode")
                    val serverVersionName = json.getString("versionName")
                    val apkUrl = json.getString("apkUrl")
                    val changelog = json.optString("changelog", "버그 수정 및 성능 향상")

                    val pInfo = ctx.packageManager.getPackageInfo(ctx.packageName, 0)
                    val currentVersionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                        pInfo.longVersionCode.toInt()
                    } else {
                        @Suppress("DEPRECATION")
                        pInfo.versionCode
                    }
                    val currentVersionName = pInfo.versionName ?: "0.0.0"

                    if (serverVersionCode > currentVersionCode) {
                        activity.runOnUiThread {
                            showUpdateDialog(activity, serverVersionName, changelog, apkUrl)
                        }
                    } else if (!silent && serverVersionCode == currentVersionCode) {
                        activity.runOnUiThread {
                            Toast.makeText(ctx, "현재 최신 버전을 사용 중입니다. ($currentVersionName)", Toast.LENGTH_SHORT).show()
                        }
                    }
                } else if (!silent) {
                    activity.runOnUiThread {
                        Toast.makeText(ctx, "서버 연결 실패", Toast.LENGTH_SHORT).show()
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
                if (!silent) {
                    activity.runOnUiThread {
                        Toast.makeText(ctx, "업데이트 확인 중 오류 발생", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }.start()
    }

    private fun showUpdateDialog(activity: Activity, version: String, changelog: String, apkUrl: String) {
        if (activity.isFinishing || activity.isDestroyed) return

        val view = LayoutInflater.from(activity).inflate(R.layout.dialog_app_update_oneui, null)
        val tvVersion = view.findViewById<TextView>(R.id.tvUpdateVersion)
        val tvChangelog = view.findViewById<TextView>(R.id.tvChangelog)
        val btnLater = view.findViewById<Button>(R.id.btnUpdateLater)
        val btnNow = view.findViewById<Button>(R.id.btnUpdateNow)

        tvVersion.text = "Version $version"
        tvChangelog.text = changelog

        val dialog = AlertDialog.Builder(activity)
            .setView(view)
            .setCancelable(true)
            .create()

        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        btnLater.setOnClickListener {
            dialog.dismiss()
        }

        btnNow.setOnClickListener {
            dialog.dismiss()
            try {
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(apkUrl)).apply {
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK
                }
                activity.startActivity(intent)
                Toast.makeText(activity, "최신 버전 다운로드 페이지로 이동합니다.", Toast.LENGTH_SHORT).show()
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(activity, "다운로드 링크 열기 실패: ${e.message}", Toast.LENGTH_SHORT).show()
            }
        }

        dialog.show()

        val width = (activity.resources.displayMetrics.widthPixels * 0.88).toInt()
        dialog.window?.setLayout(width, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)
    }
}
