package com.wasu.cs.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.wasu.cs.module.WasuCacheModule;
import com.wasu.cs.utils.DataCleanManager;
import com.wasu.cs.widget.wheelview.AboutUsUpdateView;

import basic.update.present.UpdatePresent;
import cn.com.wasu.main.R;

import static com.wasu.cs.utils.DataCleanManager.getTotalCacheSize;

/**
 * Created by chenming on 2016/9/12.
 *
 * @author chenming
 * @Description: 关于我们页面布局
 * @email chenming@wasu.com
 */

public class AboutUsLayout extends LinearLayout implements View.OnClickListener {

	private Context mContext;
	private TextView tvversion, tvcurversion, tvcachesize;
	private RelativeLayout mupdate, mclcancache, aboutwasu;
	private String cachesize;
	private String versionCode;
	private final String TAG = "AboutUsLayout";

	public AboutUsLayout(Context context) {
		super(context);
		init(context);
	}

	public AboutUsLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public AboutUsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	private void init(Context context) {
		this.mContext = context;
		LayoutInflater.from(context).inflate(R.layout.layout_aboutus, this);
		tvversion = (TextView) findViewById(R.id.tvversion);
		tvcurversion = (TextView) findViewById(R.id.tvcurversion);
		tvcachesize = (TextView) findViewById(R.id.tvcachesize);
		mupdate = (RelativeLayout) findViewById(R.id.rlupdate);
		mclcancache = (RelativeLayout) findViewById(R.id.rlcleancache);
		aboutwasu = (RelativeLayout) findViewById(R.id.aboutwasu);
		mupdate.setOnClickListener(this);
		mclcancache.setOnClickListener(this);
		aboutwasu.setOnClickListener(this);
		initAboutUs();
	}

	/**
	 * 显示版本
	 *
	 * @param verion
	 */
	public void showVersion(String verion, String cache) {
		tvversion.setText("华数TV " + verion);
		tvcurversion.setText("当前版本: " + verion);
		tvcachesize.setText("缓存大小：" + cache);
	}

	/**
	 * author chenming
	 * 初始化关于我们页面数据
	 */
	private void initAboutUs(){

		versionCode = getAppVersionName(mContext);
		try {
			cachesize = getTotalCacheSize(mContext);
		} catch (Exception e) {
			e.printStackTrace();
		}
		showVersion(versionCode, cachesize);

	}

	/**
	 * author chenming
	 * 升级app
	 */
	private void updateApp() {
		UpdatePresent.getInstance().checkWhetherNeedUpdate(new AboutUsUpdateView());
	}


	private String getAppVersionName(Context context) {
		PackageInfo packageInfo = null;
		PackageManager packageManager = context.getPackageManager();
		try {
			packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return packageInfo.versionName;
	}

	/**
	 * 清理缓存
	 */
	private void cleanCache() {
		/*AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
		builder.setTitle("清理缓存");
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setMessage(R.string.clean_cache);
		builder.create().show();*/
		//清楚fresco磁盘缓存
		Fresco.getImagePipeline().clearDiskCaches();
		WasuCacheModule.getInstance().clear();
		DataCleanManager.clearAllCache(mContext);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				try {
					cachesize = DataCleanManager.getTotalCacheSize(mContext);
					tvcachesize.setText("缓存大小："+cachesize);
					Toast.makeText(getContext(), "缓存清理成功", Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		},1500);

	}

	/**
	 * 关于华数简介
	 */
	private void showAboutWasu(){
		AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
		builder.setTitle("华数TV"+versionCode+"版本主要更新");
		builder.setMessage("1.新增生活版块，开启大屏购物新姿势；\n" +
				"2.改版少儿频道，个性化UI充满童趣；\n" +
				"3.接入咪咕学堂，点播内容更加丰富；\n" +
				"4.修复部分问题，极大提高版本稳定性.");
		builder.create().show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.rlupdate:
				updateApp();
				break;
			case R.id.rlcleancache:
				cleanCache();
				break;
			case R.id.aboutwasu:
				showAboutWasu();
				break;
			default:
				break;

		}
	}
}
