package com.usai.apex;

import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AboutActivity extends AppCompatActivity
{
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent intent = new Intent();
		switch (item.getItemId())
		{
			case android.R.id.home:
				finish();
				break;

			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void setCustomActionBar() {
		ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
		View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);

		TextView titleview = mActionBarView.findViewById(R.id.title);
		titleview.setText("About");
		setTitle("About");

		ActionBar actionBar = getSupportActionBar();
		actionBar.setCustomView(mActionBarView, lp);
//		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);


		actionBar.setDisplayShowTitleEnabled(true);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Log.d("AboutActivity", "onCreate");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);

		setCustomActionBar();
		final TextView tv_ver = (TextView) findViewById(R.id.about_ver_value_tv);


		TextView tv_pdfpreview = (TextView) findViewById(R.id.tv_androidpdfviewer);
		tv_pdfpreview.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				InputStream is = getResources().openRawResource(R.raw.license_androidpdfviewer);//把文件转换为输入流
				StringBuffer response = new StringBuffer();                //创建StringBuffer实例
				BufferedReader br = new BufferedReader(new InputStreamReader(is));  //根据is创建缓冲字符输入流
				String s = null;                            //创建s变量
				try {                            //try语句捕获异常
					while ((s = br.readLine()) != null) {       //把这一行的值赋值给变量s，并判断是否有值
						response.append(s);                        //把值添加进StringBuffer
						response.append("\n");                 //再添加一个换行符
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block          //catch异常处理
					e.printStackTrace();       //得到错误的实例， 调用方法在命令行打印程序出错的位置及原因
				} finally {              //finally try语句大多数情况下都会执行的代码块
					try {
						if (is != null) {      //如果文件输入流不为空
							is.close();            //调用close函数关掉输入流
						}
						if (br != null) {      //如果缓冲字符输入流不为空
							br.close();            //调用close函数关掉缓冲字符输入流
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				Intent intent = new Intent();
				intent.setClass(AboutActivity.this, LicenseActivity.class);

				intent.putExtra("function_name", "barteksc/AndroidPdfViewer");

				intent.putExtra("content", response.toString());


				startActivity(intent);
			}
		});
		// String str =
		// "<a href='https://ra.apexshipping.com/main.php?action=handset_search&action_type=download_doc&sessionid=hjqvgilu0o64d22qfqgat1afk2&url=QVRwVUswa3ZFNlBFTzhLbFpYcC9rVEhSZ1h0MGd6ekpwZ2ZqTkhXNnN2SytGM0FhRkhyNG5QbkdtV2tPMXZrK2p3' target=_blank>A1303540085F.pdf</a>";

//		WebView wv = (WebView) findViewById(R.id.webView1);
//		String content = "";
//		try
//		{
//			InputStream in = getResources().openRawResource(R.raw.about);
//			// 获取文件的字节数
//			int lenght = in.available();
//			// 创建byte数组
//			byte[] buffer = new byte[lenght];
//			// 将文件中的数据读到byte数组中
//			in.read(buffer);
//			content = EncodingUtils.getString(buffer, "UTF-8");
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//		}
//		wv.getSettings().setDefaultTextEncodingName("UTF-8");
//		wv.loadData(content, "text/html", null);
//

		try
		{

			tv_ver.setText(ApexTrackingApplication.get_instance()
							.getPackageManager()
							.getPackageInfo("com.usai.apex", 0).versionName);
		}
		catch (NameNotFoundException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	//
	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.about, menu);
	// return true;
	// }

}
