package com.idle.wall.idleactivity;

import android.app.WallpaperManager;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.idle.wall.IdleWallpaper;
import com.idle.wall.ad.AdsInsUtil;
import com.idle.wall.ad.LoadListener;
import com.idle.wall.ad.ShowListener;
import com.idle.wall.databinding.ActivityIdleWallBinding;
import com.idle.wall.idleadapter.OnSelecttype;
import com.idle.wall.idlebean.IdleData;
import com.idle.wall.idlefragment.Idledialog;
import com.idle.wall.idletool.Comutils;
import com.idle.wall.idletool.ObectManager;
import com.idle.wall.R;
import com.idle.wall.idletool.JsonData;
import com.idle.wall.RecentManager; // 【新增】导入最近浏览管理类
import com.tradplus.ads.base.bean.TPAdInfo;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

public class IdleWallActivity extends AppCompatActivity implements OnSelecttype {

    private static final String TAG = "PreviewActivity"; // 【新增】日志标签
    private IdleData data;
    private WallpaperManager wallpaperManager;
    private final int permissionCode = 1;
    private String mSavePath;
    private String imId;
    private String fullUrl;

    private Idledialog dialogType;
    private File downloadFile;
    private Call call;
    private ActivityIdleWallBinding vb;

    private Timer clockTimer;
    private Handler mainHandler;
    private final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd EEEE", Locale.ENGLISH);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityIdleWallBinding.inflate(getLayoutInflater());
        Comutils.initFull(this, true);
        setContentView(vb.getRoot());
        AdsInsUtil.INSTANCE.showAd(this, AdsInsUtil.Placement.TOP_ON_AD_THREE, new ShowListener() {
            @Override
            public void onAdShown(@org.jetbrains.annotations.Nullable TPAdInfo ad) {
                loadAd();
            }

            @Override
            public void onAdClosed() {
                loadAd();
            }
        });
        data = (IdleData) getIntent().getSerializableExtra(Comutils.key_info);
        if (data == null) {
            finish();
            return;
        }
        imId = data.getImId();
        mSavePath = JsonData.INSTANCE.getSaveFilePath(this, imId);
        fullUrl = data.getFullUrl();
        wallpaperManager = WallpaperManager.getInstance(this);

        mainHandler = new Handler(Looper.getMainLooper());

        addToRecentWallpapers();
        ObectManager.updateViewHistory(data);

        initClick();
        loadDetail();
        vb.tvContent.setText(data.getDescription());
        initClockWeather();
        startClockRefresh();
    }
    private void addToRecentWallpapers() {
        if (data == null) {
            return;
        }

        IdleWallpaper wallpaper = new IdleWallpaper();
        String wallpaperId = data.getImId() != null ? data.getImId() : System.currentTimeMillis() + "";
        wallpaper.setId(wallpaperId);
        String title = data.getDescription() != null ? data.getDescription() : "Wallpaper " + wallpaperId;
        wallpaper.setTitle(title);
        String imageUrl = data.getFullUrl() != null ? data.getFullUrl() : (data.getPreviewUrl1080() != null ? data.getPreviewUrl1080() : "");
        wallpaper.setImageUrl(imageUrl);
        wallpaper.setViewTime(System.currentTimeMillis());
        RecentManager.getInstance(this).addRecentWallpaper(wallpaper);
    }
    private void initClockWeather() {
        updateClockWeather();
        vb.tvWeather.setText(R.string.time3);
    }

    private void startClockRefresh() {
        if (clockTimer != null) {
            clockTimer.cancel();
        }
        clockTimer = new Timer();
        clockTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!isDestroyed() && mainHandler != null) {
                    mainHandler.post(IdleWallActivity.this::updateClockWeather);
                }
            }
        }, 0, 60 * 1000);
    }

    private void updateClockWeather() {
        Date currentTime = new Date();
        vb.tvClock.setText(timeFormat.format(currentTime));
        vb.tvDate.setText(dateFormat.format(currentTime));

        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        if (hour >= 18 || hour < 6) {
            vb.tvWeather.setText(R.string.time4);
        } else if (hour >= 12 && hour < 18) {
            vb.tvWeather.setText(R.string.time5);
        } else {
            vb.tvWeather.setText(R.string.time6);
        }
    }

    private void loadDetail() {
        String previewUrl400 = data.getPreviewUrl400();
        String previewUrl1080 = data.getPreviewUrl1080();

        File file = new File(mSavePath);
        RequestOptions options = new RequestOptions().format(DecodeFormat.PREFER_RGB_565);

        if (file.exists()) {
            Glide.with(this)
                    .load(file)
                    .apply(options)
                    .placeholder(R.mipmap.im_placeholder)
                    .error(R.mipmap.im_placeholder)
                    .transition(DrawableTransitionOptions.withCrossFade(300))
                    .fitCenter()
                    .into(vb.imageviewPreview);
        } else {
            Glide.with(this)
                    .asDrawable()
                    .load(previewUrl1080)
                    .apply(options)
                    .placeholder(R.mipmap.im_placeholder)
                    .error(R.mipmap.im_placeholder)
                    .thumbnail(Glide.with(this)
                            .asDrawable()
                            .load(previewUrl400)
                            .apply(options)
                            .placeholder(R.mipmap.im_placeholder)
                            .fitCenter())
                    .transition(DrawableTransitionOptions.withCrossFade(300))
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    })
                    .fitCenter()
                    .into(vb.imageviewPreview);
        }

        vb.imageFavorite.setSelected(ObectManager.queryIsLike(imId));
    }

    private void initClick() {
        vb.imageviewBack.setOnClickListener(v -> finish());

        vb.buttonSet.setOnClickListener(v -> {
            if (dialogType == null) {
                dialogType = new Idledialog();
                dialogType.setListener(this);
            }
            if (!dialogType.isAdded()) {
                dialogType.show(getSupportFragmentManager(), "");
            }
        });

        vb.imageFavorite.setOnClickListener(v -> {
            boolean isSelected = vb.imageFavorite.isSelected();
            vb.imageFavorite.setSelected(!isSelected);
            data.setLike(!isSelected);
            ObectManager.updateLike(data);
        });

        vb.layoutDownload.setOnClickListener(v -> {
            if (JsonData.INSTANCE.requestPermission(this, permissionCode)) {
                startSaveToAlbum();
            }
        });
    }

    private void startSaveToAlbum() {
        showDownloadLoading(true);
        if (downloadFile != null && downloadFile.exists()) {
            startSave(downloadFile);
            return;
        }
        doDownload(true, true);
    }

    private void doDownload(boolean showMsg, boolean save) {
        call = JsonData.INSTANCE.downloadFile(fullUrl, mSavePath, (aBoolean, inputStream) -> {
            File file = new File(mSavePath);
            if (aBoolean && file.exists()) {
                downloadFile = file;
                if (save) {
                    startSave(downloadFile);
                }
            } else if (showMsg) {
                runOnUiThread(() -> {
                    showDownloadLoading(false);
                    Toast.makeText(IdleWallActivity.this, getString(R.string.download_fail), Toast.LENGTH_SHORT).show();
                });
            }
            return null;
        });
    }

    private void showDownloadLoading(boolean show) {
        vb.pbDownload.setVisibility(show ? View.VISIBLE : View.GONE);
        vb.imageDownload.setVisibility(show ? View.GONE : View.VISIBLE);
    }

    private void showSetWallpaperLoading(boolean show) {
        vb.pbSet.setVisibility(show ? View.VISIBLE : View.GONE);
        vb.buttonSet.setVisibility(show ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == permissionCode && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            startSaveToAlbum();
        }
    }

    private void startSave(File file) {
        ObectManager.insertOrUpdateDownload(data);
        Uri uri = JsonData.INSTANCE.saveToGallery(this, file);
        runOnUiThread(() -> {
            showDownloadLoading(false);
            Toast.makeText(this, uri == null ? R.string.save_fail : R.string.save_success,
                    uri == null ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
        });
    }

    @Override
    public void onSelectType(int type) {
        showSetWallpaperLoading(true);
        if (downloadFile != null && downloadFile.exists()) {
            new Thread(() -> setWallpaper(downloadFile, type)).start();
        } else {
            call = JsonData.INSTANCE.downloadFile(fullUrl, mSavePath, (aBoolean, inputStream) -> {
                File file = new File(mSavePath);
                if (aBoolean && file.exists()) {
                    downloadFile = file;
                    setWallpaper(downloadFile, type);
                } else {
                    runOnUiThread(() -> {
                        showSetWallpaperLoading(false);
                        Toast.makeText(IdleWallActivity.this, R.string.set_fail, Toast.LENGTH_SHORT).show();
                    });
                }
                return null;
            });
        }
    }

    private void setWallpaper(File file, int type) {
        try (FileInputStream fis = new FileInputStream(file)) {
            switch (type) {
                case Idledialog.type_home:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        wallpaperManager.setStream(fis, null, true, WallpaperManager.FLAG_SYSTEM);
                    } else {
                        wallpaperManager.setStream(fis);
                    }
                    break;
                case Idledialog.type_lock:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        wallpaperManager.setStream(fis, null, true, WallpaperManager.FLAG_LOCK);
                    } else {
                        wallpaperManager.setStream(fis);
                    }
                    break;
                case Idledialog.type_both:
                    wallpaperManager.setStream(fis);
                    break;
            }
            if (file.exists()) {
                file.delete();
            }
            runOnUiThread(() -> {
                showSetWallpaperLoading(false);
                Toast.makeText(this, R.string.set_success, Toast.LENGTH_SHORT).show();
            });
        } catch (Exception e) {
            runOnUiThread(() -> {
                showSetWallpaperLoading(false);
                Toast.makeText(this, R.string.set_fail, Toast.LENGTH_SHORT).show();
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (clockTimer != null) {
            clockTimer.cancel();
            clockTimer = null;
        }
        if (mainHandler != null) {
            mainHandler.removeCallbacksAndMessages(null);
        }
        if (call != null) {
            call.cancel();
        }
        if (dialogType != null && dialogType.isAdded()) {
            dialogType.dismiss();
        }
    }

    private void loadAd(){
        AdsInsUtil.INSTANCE.loadAd(this, AdsInsUtil.Placement.TOP_ON_AD_THREE, new LoadListener() {
            @Override
            public void loadFailed(@NotNull String error) {

            }
        });
    }
}