package com.itant.joycity.fragment;

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.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.itant.joycity.R;
import com.itant.joycity.api.JoyUrl;
import com.itant.joycity.tool.AppTool;
import com.itant.joycity.tool.StringTool;
import com.itant.joycity.tool.ToastTool;

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

/**
 * Created by Jason on 2016/10/6.
 */
public class SecondFragment extends BaseFragment {
    private String clickMeUrl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, null);
        Button btn_click_me = (Button) view.findViewById(R.id.btn_click_me);
        btn_click_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到网址
                if (!TextUtils.isEmpty(clickMeUrl)) {
                    AppTool.openUrlWithBrowser(getActivity(), clickMeUrl);
                } else {
                    ToastTool.showShort(getActivity(), "未获取到有效链接");
                }
            }
        });

        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("进入网站");

        getClickMeUrl();
        return view;
    }

    /**
     * 获取点我链接
     */
    public void getClickMeUrl() {
        RequestParams params = new RequestParams(JoyUrl.URL_CLICK_ME);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!TextUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONObject obj = StringTool.getUsefulObjectFromArray(jsonObject, "ClickMe");
                        if (obj != null) {
                            try {
                                clickMeUrl = obj.getString("Url").trim();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    } 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() {

            }
        });
    }
}
