package com.gh.gamecenter;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gh.base.BaseActivity;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.FileUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.view.CropImageCustom;

import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class CropImageActivity extends BaseActivity {

	private CropImageCustom cropimage_custom;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				toast("上传成功");
			} else if (msg.what == 1) {
				toast("上传失败");
			} else if (msg.what == 2) {
				toast("修改太频繁，请稍后再试");
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		View contentView = View.inflate(this, R.layout.activity_cropimage, null);

		init(contentView, "图片裁剪");

		RelativeLayout reuse_actionbar = (RelativeLayout) findViewById(R.id.reuse_actionbar);

		TextView confirm = new TextView(this);
		confirm.setText("确定");
		confirm.setTextColor(0xffffffff);
		confirm.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
		int padding = DisplayUtils.dip2px(getApplicationContext(), 10);
		confirm.setPadding(padding, padding, padding, padding);
		confirm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final Dialog dialog = DialogUtils.showWaitDialog(
						CropImageActivity.this, "上传中...");
				new Thread(new Runnable() {
					@Override
					public void run() {
						String path = getCacheDir() + File.separator
								+ System.currentTimeMillis() + ".jpg";
						if (cropimage_custom.savePicture(path)) {
							// 上传图片
							JSONObject result = FileUtils.uploadFile("http://user.ghzhushou.com/v1d0/icon",
									path, TokenUtils.getToken(CropImageActivity.this));
							dialog.dismiss();
							if (result != null) {
								try {
									int statusCode = result.getInt("statusCode");
									if (statusCode == HttpStatus.SC_OK) {
										Intent data = new Intent();
										data.putExtra("url", result.getString("icon"));
										setResult(RESULT_OK, data);
										finish();
										handler.sendEmptyMessage(0);
									} else if (statusCode == HttpStatus.SC_FORBIDDEN
											&& "too frequent".equals(result.getString("detail"))) {
										handler.sendEmptyMessage(2);
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							} else {
								handler.sendEmptyMessage(1);
							}
						} else {
							dialog.dismiss();
							handler.sendEmptyMessage(1);
						}
					}
				}).start();
			}
		});
		RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		rparams.addRule(RelativeLayout.CENTER_VERTICAL);
		confirm.setLayoutParams(rparams);
		reuse_actionbar.addView(confirm);

		String path = getIntent().getStringExtra("path");

		ImageUtils.getInstance(getApplicationContext()).displayFile(
				"file://" + path, cropimage_custom.getCropImageZoomView());

	}

}
