package com.ruiwenliu.wrapper.browse;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.baidu.mapapi.model.LatLng;
import com.base.utils.base.BaseActivity;
import com.ruiwenliu.wrapper.R;
import com.ruiwenliu.wrapper.dialog.BottomPromptTwoDialog;
import com.ruiwenliu.wrapper.util.ImageFileUtil;
import com.ruiwenliu.wrapper.util.MapUtil;
import com.ruiwenliu.wrapper.util.ViewHolder;
import com.rv.component.utils.LocationRecord;

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

/**
 * 图片浏览
 * 使用方法：
 * 1.图片id集合，2.背景
 * ArrayList<String> images = new ArrayList<>();
 * images.add("url");
 * Bundle bundle = new Bundle();
 * bundle.putInt(ActivityImageBrowseInt.KEY_BACKGROUND,R.color.colorPrimary);
 * bundle.putIntegerArrayList(ActivityImageBrowseUrl.KEY_IMAGE,images);
 * IntentUtil.startActivity(baseContext,ActivityImageBrowseUrl.class,bundle);
 */

public class ActivityImageBrowseUrl2 extends BaseActivity {
    final public static String KEY_IMAGE = "image";
    final public static String KEY_BACKGROUND = "background";
    final public static String KEY_SELECTPOSITION = "select";

    FrameLayout root;
    ViewPager viewPager;
    RecyclerView recyclerView;
    ArrayList<String> urlList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_browse);
        root = findViewById(R.id.activity_image_browse_root);
        viewPager = findViewById(R.id.activity_image_browse_view_pager);
        recyclerView = findViewById(R.id.activity_image_browse_recycler_view);
        onData();
        onView();
    }

    private List<FragmentImageBrowse2> fragmentImageBrowseList;
    private AdapterRecyclerViewIndicator adapterRecyclerViewIndicator;

    @Override
    public void onData() {
        fragmentImageBrowseList = new ArrayList<>();

        int select = getIntent().getIntExtra(ActivityImageBrowseInt.KEY_SELECTPOSITION, 0);

        int background = getIntent().getIntExtra(ActivityImageBrowseInt.KEY_BACKGROUND, 0);
        if (background != 0) {
            root.setBackgroundColor(ContextCompat.getColor(baseContext, background));
        }
        urlList = getIntent().getStringArrayListExtra(ActivityImageBrowseUrl2.KEY_IMAGE);
        if (urlList != null) {
            for (String url : urlList) {
                fragmentImageBrowseList.add(FragmentImageBrowse2.getInstance(url));
            }
        }


        viewPager.setAdapter(new AdapterViewPager(getSupportFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
                adapterRecyclerViewIndicator.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



        if (fragmentImageBrowseList.size() >= 2) {
            recyclerView.setVisibility(View.VISIBLE);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(baseContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            adapterRecyclerViewIndicator = new AdapterRecyclerViewIndicator();
            recyclerView.setAdapter(adapterRecyclerViewIndicator);
        }

        viewPager.setCurrentItem(select);

        viewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new BottomPromptTwoDialog(ActivityImageBrowseUrl2.this) {
                    @Override
                    public void helper(ViewHolder helper) {
                        super.helper(helper);

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

    @Override
    public void onView() {

    }

    class AdapterViewPager extends FragmentPagerAdapter {


        public AdapterViewPager(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentImageBrowseList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentImageBrowseList.size();
        }
    }

    private static int index = 0;

    class AdapterRecyclerViewIndicator extends RecyclerView.Adapter<AdapterRecyclerViewIndicator.ViewHolderIndicator> {

        @Override
        public ViewHolderIndicator onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(baseContext).inflate(R.layout.item_image_browse_indicator_point, parent, false);
            return new ViewHolderIndicator(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolderIndicator holder, int position) {
            if (index == position) {
                holder.imageView.setImageResource(R.drawable.ic_indicator_point_select);
            } else {
                holder.imageView.setImageResource(R.drawable.ic_indicator_point_normal);
            }
        }

        @Override
        public int getItemCount() {
            return fragmentImageBrowseList.size();
        }

        class ViewHolderIndicator extends RecyclerView.ViewHolder {

            ImageView imageView;

            public ViewHolderIndicator(View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.item_image_browse_indicator_point_image_view);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        index = 0;
    }
}
