package com.xhly.manageapp

import DownLoadUtils
import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hjq.toast.Toaster
import com.xhkjedu.manageapp.R
import com.xhkjedu.manageapp.databinding.ItemAppBinding
import com.xhly.corelib.Const
import com.xhly.corelib.bean.AppInfo
import com.xhly.corelib.eventbus.UIEvent
import com.xhly.corelib.utils.AppUtils
import com.xhly.corelib.utils.LogShow
import com.xhly.corelib.utils.NetworkUtils
import com.xhly.corelib.utils.SystemUtil
import com.xhly.manageapp.utils.InstallUtils
import com.xhly.manageapp.utils.StrategyUtils
import java.util.Calendar

class AppAdapter(var context: Context, var data: ArrayList<AppInfo>) :
    RecyclerView.Adapter<AppAdapter.AppViewHolder>() {
     var changePwdListener:ChangePwdListener?=null
    var lastTimeLong=0L
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppViewHolder {
        val inflate = ItemAppBinding.inflate(LayoutInflater.from(context), parent, false)
        return AppViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        return data.size
    }




    override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
        val appInfo = data[position]
        holder.mBinding.itemCv.setImageDrawable(appInfo.ico)
        holder.mBinding.itemCv.setText(appInfo.name)
        holder.mBinding.itemCv.setOnClickListener {
            if (appInfo.packageName.equals(Const.CUSTOMSPEEDAPP)){
                StrategyUtils.clearMemory(context,data)
                Toaster.show(context.getString(R.string.speedend))
            }else if (appInfo.packageName.equals(Const.CUSTOMCLEARAPP)){
                StrategyUtils.clearAppData(context)
                Toaster.show(context.getString(R.string.clearend))
            }else if (appInfo.packageName.equals(Const.CUSTOMROTATIONAPP)){
                val rotationAllowed = SystemUtil.isRotationAllowed(context)
                ManageApplication.disallowSetAutoRotation(!rotationAllowed)
                val tips=if (!rotationAllowed){
                    context.getString(R.string.openrotation)
                }else{
                    context.getString(R.string.closerotation)
                }
                Toaster.showLong(tips)
            }else if (appInfo.packageName.equals(Const.CUSTOMCHANGEPSD)){
                changePwdListener?.let {
                    it.changeCode()
                }
            }else if (appInfo.packageName.equals(Const.UPDATESTRATEGYPKGNAME)){
               if (NetworkUtils.isAvailable(context)){
                   if (lastTimeLong+20*1000<System.currentTimeMillis()){
                       lastTimeLong=System.currentTimeMillis()
                       UIEvent(Const.CODE2004.toString()).post()
                   }else{
                       Toaster.show(context.getString(R.string.strategyupdatetips))
                   }
               }else{
                   Toaster.show(context.getString(R.string.checknettips))
               }
            }else if (appInfo.packageName.equals(Const.WIFISEETTINGPKG)){
                context.startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
            }else if (appInfo.packageName.equals(Const.STOREAPPPKG)){
                //跳转应用商店
                if (!AppUtils.isAppInstalled(context,appInfo.packageName)){
                    Toaster.show(context.getString(R.string.startapptips))
                    return@setOnClickListener
                }
                var destroyFlag=false
                try {
                   /* val launchIntent = Intent()
                    launchIntent.setComponent(
                        ComponentName(
                            Const.STOREAPPPKG,
                            Const.STOREAPPPKG+".MainActivity"
                        )
                    )
                    context.startActivity(launchIntent)*/
                    val intent: Intent? =
                        context.getPackageManager().getLaunchIntentForPackage(Const.STOREAPPPKG)
                    if (intent != null) {
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        context.startActivity(intent)
                        destroyFlag=true
                    }else{
                        destroyFlag=false
                    }
                }catch (e:Exception){
                    destroyFlag=false
                    LogShow("失败原因"+e.toString())
                }finally {
                    if (destroyFlag){
                        DownLoadUtils.destroyDownLoad()
                        InstallUtils.destroyInstallUtils()
                    }else{
                        Toaster.show(context.getString(R.string.startapptips))
                    }
                }
            } else{
                try {
                    if (!AppUtils.isAppInstalled(context,appInfo.packageName)){
                        Toaster.show("应用正在安装，请稍后")
                        return@setOnClickListener
                    }
                    var instance = Calendar.getInstance()
                    instance.set(Calendar.HOUR_OF_DAY, 11)
                    instance.set(Calendar.MINUTE, 0)
                    /* context.startActivity(appInfo.intent)*/
                    val appTimeStrategy = StrategyUtils.getAppTimeStrategy(context, appInfo.packageName)
                    if (appTimeStrategy){
                        context.startActivity(appInfo.intent)
                    }else{
                        Toaster.showLong(context.getString(R.string.apptimestoptips))
                    }
                }catch (e:Exception){
                    LogShow("应用跳转问题是"+e.toString())
                }   
            }
        }
    }

    class AppViewHolder(binding: ItemAppBinding) : RecyclerView.ViewHolder(binding.root) {
        var mBinding = binding
    }
   interface ChangePwdListener{
       fun changeCode()
   }
}