package com.miraclegarden.smsmessage.Activity;

import android.content.Intent;
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.App;
import com.miraclegarden.smsmessage.AppInfo;
import com.miraclegarden.smsmessage.AppListUtil;
import com.miraclegarden.smsmessage.DeleteConfirmDialog;
import com.miraclegarden.smsmessage.MessageInfo;
import com.miraclegarden.smsmessage.R;
import com.miraclegarden.smsmessage.comm.CommonAdapter;
import com.miraclegarden.smsmessage.comm.ViewHolder;
import com.miraclegarden.smsmessage.databinding.ActivitySettingBinding;
import com.miraclegarden.smsmessage.network.TokenManager;

import java.util.ArrayList;
import java.util.List;

public class SettingActivity extends MiracleGardenActivity<ActivitySettingBinding> {
    private ArrayList<MessageInfo> appList = new ArrayList<>();
    private CommonAdapter userAdapter;
    private TokenManager tokenManager;

    @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();
        initList();
    }

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

        binding.ivBack.setOnClickListener(v -> finish());

        binding.btnAddApp.setOnClickListener(v -> {
            startActivity(new Intent(this, AppListActivity.class));
        });

        binding.rowKeepAliveSettings.setOnClickListener(v -> {
            startActivity(new Intent(this, KeepAliveSettingsActivity.class));
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshList();
    }

    private void refreshList() {
        List<MessageInfo> list = App.getNotiList(this);
        appList = new ArrayList<>(list);
        userAdapter.setDates(appList);

        if (appList.size() > 0) {
            binding.recyclerview.setVisibility(View.VISIBLE);
            binding.tvEmpty.setVisibility(View.GONE);
        } else {
            binding.recyclerview.setVisibility(View.GONE);
            binding.tvEmpty.setVisibility(View.VISIBLE);
        }
    }

    private void initList() {
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        userAdapter = new CommonAdapter<>(this, R.layout.item_user, appList) {
            @Override
            public void convert(ViewHolder holder, MessageInfo info, int index) {
                AppInfo appInfo = AppListUtil.getAppByPackageName(SettingActivity.this, info.getPackageName());

                ImageView deleteBtn = holder.getView(R.id.delete_img);
                deleteBtn.setVisibility(View.VISIBLE);
                deleteBtn.setOnClickListener(v -> {
                    DeleteConfirmDialog dialog = new DeleteConfirmDialog(SettingActivity.this, info);
                    dialog.setOnConfirmListener(() -> {
                        App.deleteNotiBean(SettingActivity.this, info);
                        refreshList();
                    });
                    dialog.show();
                });

                if (appInfo != null) {
                    ((ImageView) holder.getView(R.id.iv_icon)).setImageDrawable(appInfo.getIcon());
                } else {
                    ((ImageView) holder.getView(R.id.iv_icon)).setImageResource(R.mipmap.app_logo);
                }

                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() + ")");
                    holder.getView(R.id.tv_remark).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.tv_remark).setVisibility(View.GONE);
                }
            }
        };

        binding.recyclerview.setAdapter(userAdapter);
    }
}
