package com.xunao.effectdemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.xunao.effectdemo.Data;
import com.xunao.effectdemo.DragGridView;
import com.xunao.effectdemo.MoreColorTypeAdapter;
import com.xunao.effectdemo.R;
import com.xunao.effectdemo.dragview.MediaView;

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

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/120:56
 * desc :
 * version: 1.0
 */
public class ListActivity extends Activity  implements AdapterView.OnItemClickListener{

	private static final int NUM_COLUMNS = 3;//grideview的列数
	private List<Data> data = new ArrayList<>();
	private MoreColorTypeAdapter mMoreColorTypeAdapter;
	private MediaView m1,m2;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		DragGridView dragGridView = findViewById(R.id.dgv);

		dragGridView.setNumColumns(NUM_COLUMNS);
		initData();
		mMoreColorTypeAdapter = new MoreColorTypeAdapter(this, true, data, NUM_COLUMNS);
		dragGridView.setAdapter(mMoreColorTypeAdapter);

		dragGridView.setOnItemClickListener(this);
		dragGridView.setOnItemChangeListener(new DragGridView.OnItemChangeListener() {
			@Override
			public void onChange(int from, int to) {
				Data dataBean = data.get(from);
				//直接交互
				//Collections.swap(dataSourceList,from,to);

				//非直接交互 这里的处理需要注意下 排序交换
				if(from < to){
					for(int i = from; i < to; i++){
						Collections.swap(data, i, i + 1);
					}
				}else if(from > to){
					for(int i = from; i > to; i--){
						Collections.swap(data, i, i - 1);
					}
				}
				data.set(to, dataBean);

				mMoreColorTypeAdapter.notifyDataSetChanged();
			}

			@Override
			public void onStop() {
				//拖动停止
			}
		});
	}

	private void initData() {
		for (int i = 0; i < 10; i++) {
			Data dataBean = new Data();
			dataBean.setImage(R.drawable.lott_icon_football);
			dataBean.setFooterType(i % 2 == 0 ? 0 : 1);
			dataBean.setName("测试数据" + i);
			dataBean.setTip(i % 2 == 0 ? null : "" + i);
			dataBean.setTag("测试tag" + i);
			data.add(dataBean);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
		Toast.makeText(ListActivity.this, i + "", Toast.LENGTH_SHORT).show();
	}
}
