package com.lemonhc.mplus.mcare_plus.activity

import android.Manifest
import android.content.Intent
import android.os.Bundle

import com.lemonhc.mplus.mcare_plus.R
import android.content.pm.PackageManager
import android.os.Environment
import androidx.core.app.ActivityCompat
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.lemonhc.mplus.framework.util.kotlinExtension.isCaptureFlag
import com.lemonhc.mplus.model.User
import org.json.JSONObject
import java.io.*


class PhotoActivity : AppCompatActivity() {

    private val cameraReturnCode = 2000
    private val gallaryReturnCode = 3000
    private var photoPath = "${Environment.getExternalStorageDirectory()}/kunkuk"

    companion object {
        lateinit var callBack:(JSONObject) -> Unit
        lateinit var user: User
                //= User("1111", "홍길동", "111", "222", "20", "M", "333", "444", "555", "김닥터", "666")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if(isCaptureFlag == false){
            window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
        }
        setContentView(R.layout.activity_)

        this@PhotoActivity.cameraAction()

        finish()


    }

    //카메라 뷰로 이동
    fun cameraAction(){
        if (checkPermissionCamera()) {

            val directory = File(this@PhotoActivity.photoPath)
            if (!directory.exists()) {
                directory.mkdirs()
            }

            //미디어스캐닝 방지
            val nomedia = File("${this@PhotoActivity.photoPath}/.nomedia")
            if(!nomedia.exists()){
                nomedia.mkdir()
            }

            val intent = Intent(this, CameraActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
            startActivity(intent)

        }
    }

    //갤러리뷰로 이동
    fun gallaryAction(){
        if (checkPermission()) {

            val intent = Intent(this@PhotoActivity, GallaryActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
            startActivity(intent)

        }
    }

    //카메라 퍼미션 확인
    fun checkPermissionCamera(): Boolean {
        if (ActivityCompat.checkSelfPermission(this@PhotoActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this@PhotoActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this@PhotoActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) && ActivityCompat.shouldShowRequestPermissionRationale(this@PhotoActivity, Manifest.permission.CAMERA)) {
                //사용자 권한 취소로 인한 권한 재요청

                ActivityCompat.requestPermissions(this@PhotoActivity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA), this@PhotoActivity.cameraReturnCode)
            } else {
                // 최초로 권한을 요청
                ActivityCompat.requestPermissions(this@PhotoActivity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA), this@PhotoActivity.cameraReturnCode)
            }
            return false
        } else {
            return true
        }
    }

    //갤러리 퍼미션 확인
    fun checkPermission(): Boolean {
        if (ActivityCompat.checkSelfPermission(this@PhotoActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this@PhotoActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                //사용자 권한 취소로 인한 권한 재요청
                ActivityCompat.requestPermissions(this@PhotoActivity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), this@PhotoActivity.gallaryReturnCode)
            } else {
                // 최초로 권한을 요청
                ActivityCompat.requestPermissions(this@PhotoActivity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), this@PhotoActivity.gallaryReturnCode)
            }
            return false
        } else {
            return true
        }
    }



}
