package com.ewei.demo.ticket;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.OpenTicketPriorityListAdapter;
import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.constants.CommonValue;
import com.ewei.sdk.entity.HttpErrorInfo;
import com.ewei.sdk.entity.NameValue;
import com.ewei.sdk.listener.RequestListener;
import com.ewei.sdk.logic.TicketLogic;
import com.ewei.sdk.utility.ToastUtils;

import java.util.List;


/**
 * Created by Jay on 2018/12/4.
 */
public class OpenTicketPriorityActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    private ListView mLvPriority;
    private OpenTicketPriorityListAdapter mPriorityListAdapter;

    private NameValue mPriority;
    private List<NameValue> mPriorityList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ewei_open_activity_ticket_priority);
        mPriority = (NameValue) getIntent().getSerializableExtra("priority");

        initControl();
        getPriorityList();

    }

    private void getPriorityList() {
        showLoadingDialog("");
        TicketLogic.getInstance().getTicketPriorityField(new RequestListener<List<NameValue>>() {
            @Override
            public void onSuccess(List<NameValue> resultData, boolean isSuccess) {
                hideLoadingDialog();
                if (resultData != null) {
                    mPriorityList = resultData;
                    mPriorityListAdapter.addList(mPriorityList);
                    mPriorityListAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpErrorInfo errorInfo) {
                if (errorInfo != null && TextUtils.isEmpty(errorInfo.error_description)) {
                    ToastUtils.showToast(errorInfo.error_description);
                } else {
                    ToastUtils.showToast("获取工单优先级失败！");
                }
            }
        });
    }

    private void initControl() {

        initTitle("优先级", CommonValue.TITLE_TYPE_FINISH);
        setFinishClick("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NameValue curnv = mPriorityListAdapter.getclickItem();
                if (curnv == null) {
                    curnv = new NameValue();
                    curnv.name = "";
                    curnv.value = "";
                }

                Intent intent = getIntent().putExtra("priority", curnv);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        mLvPriority = (ListView) findViewById(R.id.lv_ticket_priority_list);
        mPriorityListAdapter = new OpenTicketPriorityListAdapter(this, mPriority);
        mLvPriority.setAdapter(mPriorityListAdapter);
        mLvPriority.setOnItemClickListener(this);

        if (mPriority == null) {
            mPriorityListAdapter.clickItem(0);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mPriorityListAdapter.clickItem(position);
    }

}
