package com.android.chmo.ui.adpater;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
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.http.RequestCallback;
import com.android.chmo.http.response.Res;
import com.android.chmo.http.service.OrderService;
import com.android.chmo.model.OrderInfo;
import com.android.chmo.ui.activity.order.OrderDetailActivity;
import com.android.chmo.ui.activity.order.PayOrderActivity;
import com.android.chmo.ui.view.StarView;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2018/8/4.
 */

public class OrderAdapter extends CommonAdapter<OrderInfo> {
    private BaseActivity baseActivity;
    private RefreshCallback refreshCallback;

    public OrderAdapter(BaseActivity activity) {
        super(activity);
        baseActivity = activity;
    }

    public void setRefreshCallback(RefreshCallback callback){
        refreshCallback = callback;
    }

    @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_order_item, null);
            holder = new ViewHolder();
            holder.headView = (ImageView) convertView.findViewById(R.id.head);
            holder.nameView = (TextView) convertView.findViewById(R.id.name);
            holder.workView = (TextView) convertView.findViewById(R.id.work);
            holder.starView = (StarView) convertView.findViewById(R.id.star);
            holder.statusView = (TextView) convertView.findViewById(R.id.status);
            holder.timePriceView = (TextView) convertView.findViewById(R.id.time_price);
            holder.operContainer =  convertView.findViewById(R.id.operContainer);
            holder.cancelBtn = (Button) convertView.findViewById(R.id.cancel);
            holder.payBtn = (Button) convertView.findViewById(R.id.toPay);
            holder.clickView = convertView.findViewById(R.id.clickView);
            convertView.setTag(holder);
        }

        final OrderInfo info = mList.get(position);

        XUtilsImage.displayCircluar(holder.headView, HttpApi.getImgUrl(info.modelhphoto), R.mipmap.def_head);
        holder.nameView.setText(info.modelpet);
        holder.workView.setText(info.skillname);
        holder.starView.setStar(TextUtils.isEmpty(info.modelstar) ? 0 : Integer.parseInt(info.modelstar));
        holder.statusView.setText(getStateStr(info.orderstate));
        holder.timePriceView.setText(info.coin + "元/" + getHour(info.scount) + "h");
        if(info.orderstate.equals("5") ||
                info.orderstate.equals("6") ||
                info.orderstate.equals("4") ||
                info.orderstate.equals("2")) {
            holder.operContainer.setVisibility(View.GONE);
        }else {
            holder.operContainer.setVisibility(View.VISIBLE);
            if(info.orderstate.equals("0")) {
                holder.cancelBtn.setVisibility(View.VISIBLE);
                holder.payBtn.setVisibility(View.VISIBLE);
            }else {
                holder.cancelBtn.setVisibility(View.VISIBLE);
                holder.payBtn.setVisibility(View.GONE);
            }
        }
        holder.cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelOrder(info);
            }
        });
        holder.payBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, PayOrderActivity.class);
                intent.putExtra("orderPk",info.pk);
                baseActivity.openPage(intent);
            }
        });
        holder.clickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(info.orderstate.equals("0")) {
                    Intent intent = new Intent(baseActivity, PayOrderActivity.class);
                    intent.putExtra("orderPk",info.pk);
                    baseActivity.openPage(intent);
                } else {
                    Intent intent = new Intent(baseActivity, OrderDetailActivity.class);
                    intent.putExtra("orderPk",info.pk);
                    baseActivity.openPage(intent);
                }

            }
        });
        return convertView;
    }

    private int getHour(String h) {
        float f = Float.parseFloat(h);

        return (int)f;
    }

    private  String getStateStr(String state) {
        if (state.equals("0")) {
            return "待付款";
        }else if(state.equals("1")) {
            return "已付款";
        }else if(state.equals("2")) {
            return "取消订单";
        }else if(state.equals("3")) {
            return "已接单";
        }else if(state.equals("4")) {
            return "模特取消";
        }else if(state.equals("5")) {
            return "已完成";
        }else if(state.equals("6")) {
            return "订单超时";
        }
        return "";
    }

    private void cancelOrder(OrderInfo info) {
        baseActivity.showLoading();
        OrderService.cancelOrder(info.pk, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                baseActivity.hideLoading();
                Res res = new Gson().fromJson(result,Res.class);
                if (res.msg.equals("success")){
                    if(refreshCallback != null) {
                        refreshCallback.onRefresh();
                    }
                }else{
                    baseActivity.showToast("取消失败");
                }
            }

            @Override
            public void onFailure(String error) {
                baseActivity.hideLoading();
                baseActivity.showToast("取消失败");
            }
        });
    }



    class ViewHolder {
        ImageView headView;
        TextView nameView;
        TextView workView;
        StarView starView;
        TextView statusView;
        TextView timePriceView;
        View operContainer;
        Button cancelBtn;
        Button payBtn;
        View clickView;
    }

    public interface RefreshCallback {
       void onRefresh();
    }
}
