package net.moyokoo.diooto;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.gyf.immersionbar.ImmersionBar;

import net.moyokoo.diooto.config.DiootoConfig;
import net.moyokoo.diooto.config.ContentViewOriginModel;
import net.moyokoo.diooto.interfaces.CircleIndexIndicator;
import net.moyokoo.diooto.interfaces.DefaultPercentProgress;
import net.moyokoo.diooto.interfaces.IIndicator;
import net.moyokoo.diooto.interfaces.IProgress;
import net.moyokoo.diooto.interfaces.Indexface;
import net.moyokoo.diooto.tools.Fucking;
import net.moyokoo.diooto.tools.Utils;

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

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import me.panpf.sketch.Sketch;
import me.panpf.sketch.SketchImageView;

public class Diooto {
    Context mContext;

    public Diooto(Context context) {
        mContext = context;
        diootoConfig = new DiootoConfig();
    }

    private DiootoConfig diootoConfig;

    public Diooto urls(String imageUrl) {
        this.diootoConfig.setImageUrls(new String[]{imageUrl});
        return this;
    }

    public Diooto fullscreen(boolean isFullScreen) {
        this.diootoConfig.setFullScreen(isFullScreen);
        return this;
    }

    public Diooto indicatorVisibility(int visibility) {
        this.diootoConfig.setIndicatorVisibility(visibility);
        return this;
    }

    public Diooto urls(String[] imageUrls) {
        this.diootoConfig.setImageUrls(imageUrls);
        return this;
    }

    public Diooto type(int type) {
        this.diootoConfig.setType(type);
        return this;
    }

    public Diooto immersive(boolean immersive) {
        this.diootoConfig.setImmersive(immersive);
        return this;
    }

    public Diooto position(int position) {
        return position(position, 0);
    }

    public Diooto position(int position, int headerSize) {
        this.diootoConfig.setHeaderSize(headerSize);
        this.diootoConfig.setPosition(position - headerSize);
        return this;
    }

    public Diooto views(View view) {
        View[] views = new View[1];
        views[0] = view;
        return views(views);
    }


    public Diooto views(RecyclerView recyclerView, @IdRes int viewId) {
        List<View> originImageList = new ArrayList<>();
        int childCount = recyclerView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View originImage = (recyclerView.getChildAt(i)
                    .findViewById(viewId));
            if (originImage != null) {
                originImageList.add(originImage);
            }
        }
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        int firstPos = 0, lastPos = 0;
        int totalCount = layoutManager.getItemCount() - diootoConfig.getHeaderSize();
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayMan = (GridLayoutManager) layoutManager;
            firstPos = gridLayMan.findFirstVisibleItemPosition();
            lastPos = gridLayMan.findLastVisibleItemPosition();
        } else if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linLayMan = (LinearLayoutManager) layoutManager;
            firstPos = linLayMan.findFirstVisibleItemPosition();
            lastPos = linLayMan.findLastVisibleItemPosition();
        }
        fillPlaceHolder(originImageList, totalCount, firstPos, lastPos);
        View[] views = new View[originImageList.size()];
        for (int i = 0; i < originImageList.size(); i++) {
            views[i] = originImageList.get(i);
        }
        return views(views);
    }

    private void fillPlaceHolder(List<View> originImageList, int totalCount, int firstPos, int lastPos) {
        if (firstPos > 0) {
            for (int pos = firstPos; pos > 0; pos--) {
                originImageList.add(0, null);
            }
        }
        if (lastPos < totalCount) {
            for (int i = (totalCount - 1 - lastPos); i > 0; i--) {
                originImageList.add(null);
            }
        }
    }

    public Diooto isLikeWx(boolean isLikeWx) {
        diootoConfig.setLikeWx(isLikeWx);
        return this;
    }

    public Diooto views(View[] views) {
        List<ContentViewOriginModel> list = new ArrayList<>();
        for (View imageView : views) {
            ContentViewOriginModel imageBean = new ContentViewOriginModel();
            if (imageView == null) {
                imageBean.left = 0;
                imageBean.top = 0;
                imageBean.width = 0;
                imageBean.height = 0;
            } else {
                int location[] = new int[2];
                imageView.getLocationOnScreen(location);
                imageBean.left = location[0];
                if (!ImmersionBar.hasNotchScreen((Activity) mContext)) {
                    //没有刘海屏
                    imageBean.top = location[1] - Fucking.getFuckHeight(getWindow(imageView.getContext()));
                } else {
                    Log.e("有刘海啊", ImmersionBar.getNotchHeight((Activity) mContext) + "");
                    imageBean.top = location[1] - Fucking.getFuckHeight(getWindow(imageView.getContext())) + ImmersionBar.getNotchHeight((Activity) mContext);
                }
                imageBean.width = imageView.getWidth();
                imageBean.height = imageView.getHeight();
            }
            list.add(imageBean);
        }
        diootoConfig.setContentViewOriginModels(list);
        return this;
    }

    public Diooto start() {
        return start(null);
    }

    public Diooto start(Indexface indexfaceListener) {
        if (!diootoConfig.isImmersive()) {
            Window window = getWindow(mContext);
            if ((window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
                    == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
                diootoConfig.setFullScreen(true);
            }
            if (!diootoConfig.isFullScreen()) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                }
            }
        }

        if (ImageActivity.iIndicator == null) {
            if (indexfaceListener != null) {
                setIndicator(new CircleIndexIndicator(indexfaceListener));
            } else {
                setIndicator(new CircleIndexIndicator());
            }
        }
        if (ImageActivity.iProgress == null) {
            setProgress(new DefaultPercentProgress());
        }
        Log.e("我就真不信了",diootoConfig.isLikeWx()+" #### ");
        if (diootoConfig.isLikeWx()){
            ImageActivity.startImageActivity(scanForActivity(mContext), diootoConfig);
        }else {
            ImageActivity.startImageActivityNoLikeWx(scanForActivity(mContext), diootoConfig);
        }
        return this;
    }

    Window getWindow(Context context) {
        if (getAppCompActivity(context) != null) {
            return getAppCompActivity(context).getWindow();
        } else {
            return scanForActivity(context).getWindow();
        }
    }

    AppCompatActivity getAppCompActivity(Context context) {
        if (context == null) return null;
        if (context instanceof AppCompatActivity) {
            return (AppCompatActivity) context;
        } else if (context instanceof ContextThemeWrapper) {
            return getAppCompActivity(((ContextThemeWrapper) context).getBaseContext());
        }
        return null;
    }

    Activity scanForActivity(Context context) {
        if (context == null) return null;

        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return scanForActivity(((ContextWrapper) context).getBaseContext());
        }
        return null;
    }

    public Diooto setProgress(IProgress on) {
        ImageActivity.iProgress = on;
        return this;
    }

    public Diooto setIndicator(IIndicator on) {
        ImageActivity.iIndicator = on;
        return this;
    }

    public Diooto loadPhotoBeforeShowBigImage(OnLoadPhotoBeforeShowBigImageListener on) {
        onLoadPhotoBeforeShowBigImageListener = on;
        return this;
    }

    public Diooto onVideoLoadEnd(OnShowToMaxFinishListener on) {
        onShowToMaxFinishListener = on;
        return this;
    }

    public Diooto onFinish(OnFinishListener on) {
        onFinishListener = on;
        return this;
    }

    public Diooto onProvideVideoView(OnProvideViewListener on) {
        onProvideViewListener = on;
        return this;
    }

    public static OnLoadPhotoBeforeShowBigImageListener onLoadPhotoBeforeShowBigImageListener;
    public static OnShowToMaxFinishListener onShowToMaxFinishListener;
    public static OnProvideViewListener onProvideViewListener;
    public static OnFinishListener onFinishListener;

    public interface OnLoadPhotoBeforeShowBigImageListener {
        void loadView(SketchImageView sketchImageView, int position);
    }

    public interface OnProvideViewListener {
        View provideView();
    }

    public interface OnShowToMaxFinishListener {
        void onShowToMax(DragDiootoView dragDiootoView, SketchImageView sketchImageView, View progressView);
    }

    public interface OnFinishListener {
        void finish(DragDiootoView dragDiootoView);
    }

    public static void cleanMemory(@NonNull Context context) {
        Sketch.with(context).getConfiguration().getDiskCache().clear();
        Sketch.with(context).getConfiguration().getBitmapPool().clear();
        Sketch.with(context).getConfiguration().getMemoryCache().clear();
    }
}
