package com.android.chmo.ui.adpater;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.model.ModelZone;
import com.android.chmo.ui.activity.model.ModelZoneDetailActivity;
import com.android.chmo.ui.activity.model.ShowImageActivity;
import com.android.chmo.ui.view.ImageGroupView;
import com.android.chmo.utils.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class ModelZoneAdapter extends CommonAdapter<ModelZone> {
    private BaseActivity baseActivity;

    public ModelZoneAdapter(BaseActivity activity) {
        super(activity);
        baseActivity = activity;
    }
    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null != convertView) {
            holder = (ViewHolder) convertView.getTag();
        }
        if(null == holder) {
            convertView = mInflater.inflate(R.layout.list_zone_item, null);
            holder = new ViewHolder();
            holder.timeContent = convertView.findViewById(R.id.timeContent);
            holder.timeView = (TextView) convertView.findViewById(R.id.time);
            holder.monthView = (TextView) convertView.findViewById(R.id.month);
            holder.imgContainer = convertView.findViewById(R.id.imgContent);
            holder.textContainer = convertView.findViewById(R.id.textContent);
            holder.imageGroupView = (ImageGroupView) convertView.findViewById(R.id.imageGroup);
            holder.infoView = (TextView) convertView.findViewById(R.id.info);
            holder.textInfoView = (TextView) convertView.findViewById(R.id.textInfo);
            convertView.setTag(holder);
        }

        final ModelZone zone = mList.get(position);

        if(zone.isditto == 0) {
            holder.timeContent.setVisibility(View.VISIBLE);
            if(isToday(zone.pdate)) {
                holder.timeView.setText("今天");
                holder.monthView.setText("");
            } else {
                String[] dateArray = getMonthDay(zone.pdate);
                holder.timeView.setText(dateArray[1]);
                holder.monthView.setText(dateArray[0]);
            }
        }else {
            holder.timeContent.setVisibility(View.INVISIBLE);
        }

        if(zone.iteminfo != null && zone.iteminfo.size() > 0) {
            holder.imgContainer.setVisibility(View.VISIBLE);
            holder.textContainer.setVisibility(View.GONE);
            holder.infoView.setText(zone.intro);
            holder.imageGroupView.setImageList(getImgList(zone.iteminfo));
        }else {
            holder.textContainer.setVisibility(View.VISIBLE);
            holder.imgContainer.setVisibility(View.GONE);
            holder.textInfoView.setText(zone.intro);
        }

        holder.imageGroupView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, ShowImageActivity.class);
                intent.putStringArrayListExtra("images", getImgList(zone.iteminfo));
                intent.putExtra("zone", zone);
                baseActivity.openPage(intent);
            }
        });
        holder.textContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, ModelZoneDetailActivity.class);
                intent.putExtra("zone", zone);
                baseActivity.openPage(intent);
            }
        });

        holder.imgContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, ModelZoneDetailActivity.class);
                intent.putExtra("zone", zone);
                baseActivity.openPage(intent);
            }
        });
        return convertView;
    }

    private boolean isToday(String time) {
        Date date = DateUtils.parseDateTime(time);
        String dateStr = DateUtils.formatDate(date);
        String curDateStr = DateUtils.formatDate(new Date());
        return  dateStr.equals(curDateStr);
    }

    private String[] getMonthDay(String time) {
        Date date = DateUtils.parseDateTime(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        String month = String.valueOf(calendar.get(Calendar.MONTH)+1)+"月";
        String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        String[] str = new String[]{month, day};
        return str;
    }

    public ArrayList<String> getImgList(List<ModelZone.ZoneItem> items) {
        ArrayList<String> list = new ArrayList<>();
        for(ModelZone.ZoneItem item : items) {
            list.add(HttpApi.getImgUrl(item.url));
        }
        return  list;
    }


    class ViewHolder {
        View timeContent;
        TextView timeView;
        TextView monthView;
        View imgContainer;
        View textContainer;
        ImageGroupView imageGroupView;
        TextView infoView;
        TextView textInfoView;
    }
}
