package ai.pai.client.adapter;

import android.content.Context;
import android.content.Intent;
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.ImageView;
import android.widget.TextView;

import com.android.common.utils.TimeUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.activity.PhotoDetailsActivity;
import ai.pai.client.beans.OrderItem;
import ai.pai.client.utils.PhotoLoader;

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderViewHolder> {

    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<OrderItem> orderList;
    private DisplayImageOptions options;

    public OrderListAdapter(Context context, ArrayList<OrderItem> groupList){
        this.context = context;
        this.orderList = groupList;
        mInflater = LayoutInflater.from(context);
        options = PhotoLoader.getPhotoOptions();
    }


    @Override
    public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.order_list_item,parent,false);
        OrderViewHolder viewHolder = new OrderViewHolder(view);
        viewHolder.thumbnail = (ImageView)view.findViewById(R.id.iv_photo_thumbnail);
        viewHolder.orderTime = (TextView)view.findViewById(R.id.tv_order_time);
        viewHolder.orderNum = (TextView)view.findViewById(R.id.tv_order_no);
        viewHolder.orderFee = (TextView)view.findViewById(R.id.tv_order_fee);
        viewHolder.itemView = view;
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OrderViewHolder holder, int position) {
        final OrderItem info = orderList.get(position);
        holder.orderNum.setText(context.getString(R.string.order_num,info.orderId));
        if(info.payStatus==0 && !TextUtils.isEmpty(info.payTime)){
            holder.orderTime.setText(context.getString(R.string.order_pay_time ,TimeUtils.getStandardTime(info.payTime)));
        }else{
            holder.orderTime.setText(context.getString(R.string.order_create_time ,TimeUtils.getStandardTime(info.createTime)));
        }
        holder.orderFee.setText("￥"+String.format("%1$6.2f",info.totalFee/100.0f));
        PhotoLoader.getInstance(context).displayImage(info.photoItem.thumbnailUrl,holder.thumbnail,options);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PhotoDetailsActivity.class);
                intent.putExtra("photo_item",info.photoItem);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return orderList ==null ? 0 : orderList.size();
    }

    public synchronized void clearOrderList(){
        this.orderList.clear();
        notifyDataSetChanged();
    }


    public synchronized void addOrderList(ArrayList<OrderItem> orderList){
        this.orderList.addAll(orderList);
        notifyDataSetChanged();
    }

    public ArrayList<OrderItem> getOrderList(){
        return orderList;
    }

    static class OrderViewHolder extends RecyclerView.ViewHolder{

        private ImageView thumbnail;
        private TextView orderTime;
        private TextView orderNum;
        private TextView orderFee;
        private View itemView;

        public OrderViewHolder(View itemView) {
            super(itemView);
        }
    }
}
