package com.miraclegarden.smsmessage.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.miraclegarden.library.app.MiracleGardenActivity;
import com.miraclegarden.smsmessage.ActionConfirmDialog;
import com.miraclegarden.smsmessage.App;
import com.miraclegarden.smsmessage.AppInfo;
import com.miraclegarden.smsmessage.AppListUtil;
import com.miraclegarden.smsmessage.R;
import com.miraclegarden.smsmessage.comm.CommonAdapter;
import com.miraclegarden.smsmessage.comm.ViewHolder;
import com.miraclegarden.smsmessage.databinding.AppListSettingBinding;
import com.miraclegarden.smsmessage.network.TokenManager;

import java.util.ArrayList;

public class AppListActivity extends MiracleGardenActivity<AppListSettingBinding> {
    private TokenManager tokenManager;
    private ArrayList<AppInfo> appList = new ArrayList<>();
    CommonAdapter userAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tokenManager = TokenManager.getInstance(this);
        // 调试用：暂时跳过登录
//        if (!tokenManager.isLoggedIn()) {
//            startActivity(new Intent(this, LoginActivity.class));
//            finish();
//            return;
//        }

        initView();
    }

    private void initView() {
        String username = tokenManager.getUsername();
        if (username != null) {
            binding.tvUsername.setText(username);
        }

        binding.backIv.setOnClickListener(view -> finish());
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        initAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        new LoadAppTask().execute();

    }

    // 异步加载
    private class LoadAppTask extends AsyncTask<Void, Void, ArrayList<AppInfo>> {
        @Override
        protected ArrayList<AppInfo> doInBackground(Void... voids) {
            // 获取全部APP
            return AppListUtil.getAllInstalledApps(AppListActivity.this);

        }

        @Override
        protected void onPostExecute(ArrayList<AppInfo> result) {
            super.onPostExecute(result);
            appList.clear();
            appList.addAll(result);
            userAdapter.notifyDataSetChanged();
            if(appList.size()<=1){
                binding.oneLy.setVisibility(View.VISIBLE);
            }else{
                binding.oneLy.setVisibility(View.GONE);
            }
            binding.loadingLy.setVisibility(View.GONE);
        }
    }



    private void initAdapter() {
        userAdapter = new CommonAdapter<>(AppListActivity.this, R.layout.item_user, appList) {
            @Override
            public void convert(ViewHolder holder, AppInfo info, int index) {
                info = App.getAppInfoByNotiList(AppListActivity.this,info);
                ((ImageView) holder.getView(R.id.iv_icon)).setImageDrawable(info.getIcon());
                holder.setText(R.id.tv_appname, info.getAppName());
                holder.setText(R.id.tv_package, info.getPackageName());

                if (!TextUtils.isEmpty(info.getName())) {
                    holder.setText(R.id.tv_name, info.getName()+"/");
                }else{
                    holder.setText(R.id.tv_name, "--/");
                }
                if (!TextUtils.isEmpty(info.getCode())) {
                    holder.setText(R.id.tv_code, info.getCode()+"/");
                }else{
                    holder.setText(R.id.tv_code, "--/");
                }
                if (!TextUtils.isEmpty(info.getRemark())) {
                    holder.setText(R.id.tv_remark, info.getRemark());
                }else{
                    holder.setText(R.id.tv_remark, "--");
                }
                AppInfo finalInfo = info;
                holder.getView(R.id.layout_big).setOnClickListener(view -> {
                        ActionConfirmDialog actionConfirmDialog = new ActionConfirmDialog(AppListActivity.this, finalInfo);
                        actionConfirmDialog.setOnToActionListener(new ActionConfirmDialog.OnToActionListener() {
                            @Override
                            public void toSumbit(AppInfo appInfo) {
                                appList.set(index,appInfo);
                                userAdapter.notifyItemChanged(index,appInfo);
                                App.saveNotiBean(AppListActivity.this,appInfo);
                            }

                            @Override
                            public void toCancel() {

                            }
                        });
                        actionConfirmDialog.show();
                    });
                }
            };

        binding.recyclerview.setAdapter(userAdapter);
        }


}
