package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.common.ui.view.CircleImageView;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
import com.tofly.ghrq_route.module.user.controller.UserController;
import com.tofly.ghrq_route.module.user.domain.DeviceBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by zombiexq on 2016/6/28.
 */
public class ReportedAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<TroubleBean> troublelist;
    private boolean mShowStatus = false;
    private ImageLoader imageLoader;
    private UserController userController;
    private Context mContext;

    public class ViewHolder {
        public TextView user;
        public TextView date;
        public TextView time;
        public TextView status;
        public TextView addr;
        public TextView clearState;
        private ImageView imageViewStatus;
        private CircleImageView imageViewUserIcon;
    }

    public ReportedAdapter(Context context, List<TroubleBean> troublelist, boolean showStatus){
        this.inflater = LayoutInflater.from(context);
        this.troublelist = troublelist;
        this.mShowStatus = showStatus;
        mContext = context;
        imageLoader = ImageLoader.getInstance();
        userController = new UserController(context);
    }

    public void setData(List<TroubleBean> troublelist){
        this.troublelist = troublelist;
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int position) {
        return troublelist.get(position - 1);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int p, View v, ViewGroup parent) {
        // init tpl
        ViewHolder holder = null;
        // if cached expired
        if (v == null) {
            v = inflater.inflate(R.layout.listview_reported_item, null);
            holder = new ViewHolder();
            holder.user= (TextView) v.findViewById(R.id.reported_user);
            holder.clearState = (TextView) v.findViewById(R.id.text_clear_state);
            holder.date = (TextView) v.findViewById(R.id.reported_date);
            holder.time= (TextView) v.findViewById(R.id.reported_time);
            holder.addr = (TextView) v.findViewById(R.id.reported_addr);
            holder.imageViewStatus = (ImageView) v.findViewById(R.id.imageview_status);
            holder.imageViewUserIcon = (CircleImageView) v.findViewById(R.id.imageview_user_icon);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        TroubleBean troubleBean = troublelist.get(p);
        String troubleName = troubleBean.getTroubleName();
        if (troubleName != null && !troubleName.isEmpty())
            holder.user.setText(troubleName);

        holder.addr.setText(troublelist.get(p).getAddress());
        String status = troublelist.get(p).getSave_state() + "";
        holder.imageViewStatus.setVisibility(View.INVISIBLE);
        if (mShowStatus){
            if (status.equals("-1")){
                holder.imageViewStatus.setVisibility(View.VISIBLE);
            }else {
                holder.imageViewStatus.setVisibility(View.INVISIBLE);
            }
        }
        if (status.equals("-1")){
            UserBean user = BaseApp.getInstance().getLoginUser();
            String faceurl = NetRequestURL.getHttpHead() + user.getAvatar();
            DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                    .showImageOnFail(R.mipmap.ico_user_name).build();
            imageLoader.displayImage(faceurl, holder.imageViewUserIcon, imageOptions);
        }

        String originTime = troubleBean.getFindTime();;
        if (originTime != null && !originTime.isEmpty()){
            String formatTime[] = new String[2];
            DateUtils.formatTime(originTime, formatTime);
            holder.date.setText(formatTime[0]);
            holder.time.setText(formatTime[1]);
        }else {
            holder.date.setVisibility(View.GONE);
            holder.time.setVisibility(View.GONE);
        }
        return v;
    }

    private void getUserInfo(CircleImageView view, String userid){
        if (userController == null)
            userController = new UserController(mContext);
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        final WeakReference<CircleImageView> weakReference = new WeakReference<CircleImageView>(view);
        userController.getUserInfo(userid, userBean.getToken(),new UserCallBack(){
            @Override
            public void onGetUserInfo(boolean isSuccess, int errorCode, String errMsg, UserBean user, DeviceBean device) {
                super.onGetUserInfo(isSuccess, errorCode, errMsg, user, device);
                CircleImageView view = weakReference.get();
                if (view != null){
                    if (isSuccess && user != null){
                        String faceurl = NetRequestURL.getHttpHead() + user.getAvatar();
                        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                                .showImageOnFail(R.mipmap.ico_user_name).build();
                        imageLoader.displayImage(faceurl, view, imageOptions);
                    }else {
                        view.setImageResource(R.mipmap.ico_user_name);
                    }
                }
            }
        });
    }
}
