/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.micode.notes.gtask.remote;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import net.micode.notes.R;
import net.micode.notes.ui.NotesListActivity;
import net.micode.notes.ui.NotesPreferenceActivity;

/**
 * Google Tasks同步异步任务类
 * 负责在后台执行Google Tasks数据同步操作，并通过通知显示同步进度和结果
 * 继承自AsyncTask，实现异步操作与UI线程的交互
 */
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {

    // 同步通知的ID和渠道ID
    private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
    private static final String NOTIFICATION_CHANNEL_ID = "gtask_sync_channel";

    /**
     * 同步完成监听器接口
     * 用于在同步任务完成后通知调用者
     */
    public interface OnCompleteListener {
        void onComplete();
    }

    // 成员变量
    private Context mContext;                  // 应用上下文
    private NotificationManager mNotifiManager;// 通知管理器
    private GTaskManager mTaskManager;        // Google Tasks管理类实例
    private OnCompleteListener mOnCompleteListener; // 完成监听器

    /**
     * 构造函数
     * @param context 应用上下文
     * @param listener 同步完成监听器
     */
    public GTaskASyncTask(Context context, OnCompleteListener listener) {
        mContext = context;
        mOnCompleteListener = listener;
        // 获取通知管理器实例
        mNotifiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        // 获取GTaskManager单例实例
        mTaskManager = GTaskManager.getInstance();
        // 创建通知渠道（适用于Android 8.0+）
        createNotificationChannel();
    }

    /**
     * 取消同步操作
     */
    public void cancelSync() {
        mTaskManager.cancelSync();
    }

    /**
     * 发布同步进度消息
     * @param message 进度消息
     */
    public void publishProgess(String message) {
        publishProgress(new String[]{message});
    }

    /**
     * 创建通知渠道（Android 8.0+需要）
     */
    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 渠道名称和描述
            CharSequence name = mContext.getString(R.string.channel_gtask_sync);
            String description = mContext.getString(R.string.channel_gtask_sync_desc);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            // 创建通知渠道
            NotificationChannel channel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, name, importance);
            channel.setDescription(description);

            // 将渠道添加到通知管理器
            if (mNotifiManager != null) {
                mNotifiManager.createNotificationChannel(channel);
            }
        }
    }

    /**
     * 显示同步通知
     * @param tickerId 通知提示文本的资源ID
     * @param content 通知内容
     */
    private void showNotification(int tickerId, String content) {
        // 定义PendingIntent（设置FLAG_IMMUTABLE以提高安全性）
        PendingIntent pendingIntent;
        if (tickerId != R.string.ticker_success) {
            // 同步失败或进行中时，点击通知跳转到设置页面
            Intent intent = new Intent(mContext, NotesPreferenceActivity.class);
            pendingIntent = PendingIntent.getActivity(
                    mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        } else {
            // 同步成功时，点击通知跳转到笔记列表页面
            Intent intent = new Intent(mContext, NotesListActivity.class);
            pendingIntent = PendingIntent.getActivity(
                    mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        }

        // 使用NotificationCompat构建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
                .setContentTitle(mContext.getString(R.string.app_name))  // 通知标题
                .setContentText(content)                               // 通知内容
                .setContentIntent(pendingIntent)                       // 点击通知后的操作
                .setSmallIcon(R.drawable.icon_app)                     // 通知图标
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)       // 通知优先级
                .setAutoCancel(true);                                   // 点击后自动取消通知

        Notification notification = builder.build();

        // 显示通知
        if (mNotifiManager != null) {
            mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
        }
    }

    /**
     * 在后台线程执行同步任务
     * @param unused 未使用的参数
     * @return 同步结果状态码
     */
    @Override
    protected Integer doInBackground(Void... unused) {
        // 发布登录进度通知
        publishProgess(mContext.getString(R.string.sync_progress_login,
                NotesPreferenceActivity.getSyncAccountName(mContext)));
        // 调用GTaskManager执行同步操作，并返回结果
        return mTaskManager.sync(mContext, this);
    }

    /**
     * 更新同步进度（在UI线程执行）
     * @param progress 进度消息数组
     */
    @Override
    protected void onProgressUpdate(String... progress) {
        // 显示同步中通知
        showNotification(R.string.ticker_syncing, progress[0]);
        // 如果上下文是GTaskSyncService，发送广播通知进度
        if (mContext instanceof GTaskSyncService) {
            ((GTaskSyncService) mContext).sendBroadcast(progress[0]);
        }
    }

    /**
     * 同步任务完成后执行（在UI线程执行）
     * @param result 同步结果状态码
     */
    @Override
    protected void onPostExecute(Integer result) {
        // 根据不同的同步结果显示相应的通知
        if (result == GTaskManager.STATE_SUCCESS) {
            showNotification(R.string.ticker_success, mContext.getString(
                    R.string.success_sync_account, mTaskManager.getSyncAccount()));
            // 保存最后同步时间
            NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
        } else if (result == GTaskManager.STATE_NETWORK_ERROR) {
            showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
        } else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
            showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
        } else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
            showNotification(R.string.ticker_cancel, mContext
                    .getString(R.string.error_sync_cancelled));
        }
        // 通知监听器同步完成
        if (mOnCompleteListener != null) {
            // 在新线程中执行回调，避免阻塞UI线程
            new Thread(() -> mOnCompleteListener.onComplete()).start();
        }
    }
}