package com.itant.joycity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
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.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itant.joycity.R;
import com.itant.joycity.adapter.GridImageTextAdapter;
import com.itant.joycity.api.JoyUrl;
import com.itant.joycity.bean.GridImageTextBean;
import com.itant.joycity.tool.AppTool;
import com.itant.joycity.tool.StringTool;
import com.itant.joycity.tool.ToastTool;
import com.itant.joycity.view.MyGridView;

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.Iterator;
import java.util.List;

/**
 * Created by Jason on 2016/10/6.
 */
public class ThirdFragment extends BaseFragment {


    private Activity activity;
    private LinearLayout ll_third;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_third, null);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("备用网址");


        activity = getActivity();
        ll_third = (LinearLayout) view.findViewById(R.id.ll_third);

        getBackupUrls();
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);


    }

    private void getBackupUrls() {
        RequestParams params = new RequestParams(JoyUrl.URL_BACKUP);
        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("BackUpUrl");
                            if (jsonArray != null && jsonArray.length() >= 0) {

                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject jsonObj = (JSONObject)jsonArray.get(i);
                                    if (jsonObj != null) {

                                        Iterator<String> keys = jsonObj.keys();
                                        while (keys.hasNext()) {
                                            String key = keys.next();
                                            JSONArray jinShaArray = jsonObj.getJSONArray(key);
                                            final List<GridImageTextBean> jinShas = StringTool.stringToList(jinShaArray.toString(), GridImageTextBean.class);
                                            if (jinShas != null) {

                                                View view = LayoutInflater.from(activity).inflate(R.layout.adapter_adapter_url, null);

                                                TextView tv_ladder_name = (TextView) view.findViewById(R.id.tv_ladder_name);
                                                tv_ladder_name.setText(key);
                                                MyGridView gv_jin_sha = (MyGridView) view.findViewById(R.id.gv_jin_sha);
                                                List<GridImageTextBean> jinShaList = new ArrayList<>();
                                                GridImageTextAdapter jinShaAdapter = new GridImageTextAdapter(getActivity(), jinShaList);
                                                gv_jin_sha.setAdapter(jinShaAdapter);
                                                gv_jin_sha.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                    @Override
                                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                        GridImageTextBean item = jinShas.get(position);
                                                        String url = item.getUrl();
                                                        if (!TextUtils.isEmpty(url)) {
                                                            AppTool.openUrlWithBrowser(getActivity(), url);
                                                        } else {
                                                            ToastTool.showShort(getActivity(), "网址为空");
                                                        }
                                                    }
                                                });
                                                jinShaList.addAll(jinShas);
                                                jinShaAdapter.notifyDataSetChanged();
                                                ll_third.addView(view);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } 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() {

            }
        });
    }
}
