package com.itant.joycity.fragment.first;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.itant.joycity.R;
import com.itant.joycity.activity.OfficialDetailActivity;
import com.itant.joycity.adapter.OfficialAdapter;
import com.itant.joycity.api.JoyUrl;
import com.itant.joycity.bean.NewestImage;
import com.itant.joycity.bean.OfficialItem;
import com.itant.joycity.fragment.BaseFragment;
import com.itant.joycity.tool.StringTool;
import com.itant.joycity.tool.ToastTool;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jason on 2016/10/6.
 */
public class OfficialFragment extends BaseFragment {
    private OfficialAdapter mAdapter;
    private List<OfficialItem> officialItems;
    private Activity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = getActivity();

        View view = inflater.inflate(R.layout.fragment_official, null);
        ListView lv_official = (ListView) view.findViewById(R.id.lv_official);
        officialItems = new ArrayList<>();

        mAdapter = new OfficialAdapter(getActivity(), officialItems);
        lv_official.setAdapter(mAdapter);

        lv_official.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                OfficialItem item = officialItems.get(position);
                if (item != null) {
                    // 文章详情
                    Intent intent = new Intent(mActivity, OfficialDetailActivity.class);
                    intent.putExtra("article", item);
                    startActivity(intent);
                }
            }
        });

        initArticles();
        return view;
    }

    private void initArticles() {
        RequestParams params = new RequestParams(JoyUrl.URL_ARTICLE);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!TextUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject != null) {
                            JSONArray jsonArray = jsonObject.getJSONArray("Article");
                            if (jsonArray != null) {
                                List<OfficialItem> items = StringTool.stringToList(jsonArray.toString(), OfficialItem.class);
                                if (items != null) {
                                    if (officialItems != null && officialItems.size() > 0) {
                                        officialItems.clear();
                                    }
                                    officialItems.addAll(items);
                                    mAdapter.notifyDataSetChanged();
                                }
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastTool.showShort(getActivity(), "获取数据失败");
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                //Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
