package com.ewei.demo.ticket;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ewei.demo.R;
import com.ewei.demo.ticket.adapter.TicketListAdapter;
import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.base.EweiDeskInfo;
import com.ewei.sdk.constants.TicketValue;
import com.ewei.sdk.entity.HttpErrorInfo;
import com.ewei.sdk.entity.Ticket;
import com.ewei.sdk.entity.TicketListResult;
import com.ewei.sdk.http.TicketHttp;
import com.ewei.sdk.listener.RequestListener;
import com.ewei.sdk.utility.LogUtils;
import com.ewei.sdk.utility.ToastUtils;

public class TicketListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = TicketListActivity.class.getSimpleName();

    private ListView mTicketList;
    private TicketListAdapter mTicketListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket_list);

        if (EweiDeskInfo.getUser() == null || TextUtils.isEmpty(EweiDeskInfo.getUserId())) {
            ToastUtils.showToast("请先获取用户信息");
            finish();
        }

        initControl();
        requestTicketList();
    }

    private void requestTicketList() {
        TicketHttp.getInstance().requestClientTicketList(1, 50, null, new RequestListener<TicketListResult>() {
            @Override
            public void onSuccess(TicketListResult resultData, boolean isSuccess) {
                if (resultData != null && resultData.list != null) {
                    mTicketListAdapter.addList(resultData.list);
                }
            }

            @Override
            public void onFailure(HttpErrorInfo errorInfo) {
                LogUtils.error(TAG, errorInfo);
                ToastUtils.showToast(errorInfo == null ? "获取用户工单列表失败" : errorInfo.error_description);
            }
        });
    }

    private void initControl() {

        initTitle("工单列表");

        mTicketList = (ListView) findViewById(R.id.ptr_ticket_view_list);
        mTicketListAdapter = new TicketListAdapter(this);
        mTicketList.setAdapter(mTicketListAdapter);
        mTicketList.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Ticket ticket = (Ticket) parent.getAdapter().getItem(position);
        if (ticket != null) {
            TicketActivity.startTicketActivity(this, ticket, TicketActivity.class, 0);
        }
    }
}
