package com.xplora.xpsettings.Activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
//import android.os.SystemProperties;
import android.os.Environment;
import android.os.StatFs;
import android.os.SystemProperties;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Utils.Constant;
import com.xplora.xpsettings.Utils.ResUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;


public class AboutActivity extends BaseActionActivity {
    private int mClickCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_title);
        initData();
        initView();
    }

    private void initData() {
        String modelName = Build.MODEL;
        String sn = DataManager.getSN();
        String imei = DataManager.getIMEI();
        String softwareVersion = Build.ID; //SystemProperties.get("ro.product.version.release");

        String[] stringList = ResUtils.getStringArray("about_title_array_", 5);
        String[] valueList = {modelName, sn, imei, softwareVersion, "", ""};

        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.subTitle = valueList[i];
            model.isShowArrow = false;
            if (i == stringList.length-1) {
                model.cellType = Constant.CellType.TITLE;
            } else {
                model.cellType = Constant.CellType.TITLE_SUBTITLE;
            }
            mDataList.add(model);
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(AboutActivity.this, 0, mDataList);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(mAdapter);
        listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(0);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 3) {
                    mClickCount++;
                    if (mClickCount >= 10) {
                        mClickCount = 0;
                        showBlackdoorPassword();
                    }
                }
            }
        });
    }

    public String getTotalInternalMemorySize(Context context) {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        //return totalBlocks * blockSize;
        return Formatter.formatFileSize(context, totalBlocks * blockSize);
    }

    public static String getTotalMemory(Context context) {
        String str1 = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;
        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小
            arrayOfString = str2.split("\\s+");
            // 获得系统总内存，单位是KB
            int i = Integer.valueOf(arrayOfString[1]).intValue();
            //int值乘以1024转换为long类型
            initial_memory = new Long((long)i*1024);
            localBufferedReader.close();
        } catch (IOException e) {

        }
        return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB，内存大小规格化
    }

    private void showBlackdoorPassword() {
        Intent intent = new Intent(this, BlackDoorPasswordActivity.class);
        intent.putExtra("from", 0);
        startActivity(intent);
    }

    private void openSourceAnnouncement() {
        Intent intent = new Intent(this, SourceAnnouncementActivity.class);
        startActivity(intent);
    }

    private void openPrivacyPolicy() {
        Intent intent = new Intent(this, PrivacyPolicyActivity.class);
        startActivity(intent);
    }
}