package kz.arta.synergy.workflow.mobile.android.works.activities.completion.process;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Map;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.works.manager.completion.SignManager;

/**
 * Created by neox on 16.02.16.
 * Экран информации о сертификате
 */
public class CertInfoActivity extends Activity {

    /**
     * Активити
     */
    private Activity activity;

    /**
     * Путь до файла с ЭЦП
     */
    private String pathToEDS;

    /**
     * Пароль для текущей ЭЦП
     */
    private String password;

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

        setContentView(R.layout.cert_info_activity);

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        activity = this;

        pathToEDS = getIntent().getStringExtra(ProcessConst.CERT_PATH);
        password = getIntent().getStringExtra(ProcessConst.CERT_PASSWORD);

        if (pathToEDS == null || password == null) {
            return;
        }

        init();
    }

    /**
     * Инициализация компонентов
     */
    private void init() {

        // заголовки
        TextView iinTitle = ((TextView) findViewById(R.id.cert_information_iin_title));
        iinTitle.setTypeface(Fonts.getRoman(activity));

        TextView nameTitle = ((TextView) findViewById(R.id.cert_information_name_title));
        nameTitle.setTypeface(Fonts.getRoman(activity));

        TextView patronymicTitle = ((TextView) findViewById(R.id.cert_information_patronymic_title));
        patronymicTitle.setTypeface(Fonts.getRoman(activity));

        TextView companyTitle = ((TextView) findViewById(R.id.cert_information_company_title));
        companyTitle.setTypeface(Fonts.getRoman(activity));

        TextView expiredTitle = ((TextView) findViewById(R.id.cert_information_expired_title));
        expiredTitle.setTypeface(Fonts.getRoman(activity));

        // иин
        TextView iin = (TextView) findViewById(R.id.cert_information_iin);
        iin.setTypeface(Fonts.getRoman(activity));

        // имя
        TextView name = (TextView) findViewById(R.id.cert_information_name);
        name.setTypeface(Fonts.getRoman(activity));

        // отчество
        TextView patronymic = (TextView) findViewById(R.id.cert_information_patronymic);
        patronymic.setTypeface(Fonts.getRoman(activity));

        // имя организации
        TextView company = (TextView) findViewById(R.id.cert_information_company);
        company.setTypeface(Fonts.getRoman(activity));

        // дата окончания действия сертификата
        TextView expired = (TextView) findViewById(R.id.cert_information_expired);
        expired.setTypeface(Fonts.getRoman(activity));

        // получение информации о сертификате
        SignManager signManager = new SignManager();
        signManager.setFilePath(pathToEDS);
        signManager.setPassword(password);
        Map<String, String> info = signManager.getCertInfo();

        // выставление текста в компоненты
        if (info.get(SignManager.SERIALNUMBER) == null) {
            iinTitle.setVisibility(View.GONE);
            iin.setVisibility(View.GONE);
        } else {
            iin.setText(info.get(SignManager.SERIALNUMBER));
        }
        if (info.get(SignManager.SURNAME) == null) {
            nameTitle.setVisibility(View.GONE);
            name.setVisibility(View.GONE);
        } else {
            name.setText(info.get(SignManager.SURNAME));
        }
        if (info.get(SignManager.GIVENNAME) == null) {
            patronymicTitle.setVisibility(View.GONE);
            patronymic.setVisibility(View.GONE);
        } else {
            patronymic.setText(info.get(SignManager.GIVENNAME));
        }
        if (info.get(SignManager.ORGANIZATION) == null) {
            company.setVisibility(View.GONE);
            companyTitle.setVisibility(View.GONE);
        } else {
            company.setText(info.get(SignManager.ORGANIZATION).replaceAll("\\\\", ""));
        }
        if (info.get(SignManager.EXPIRED) == null) {
            expiredTitle.setVisibility(View.GONE);
            expired.setVisibility(View.GONE);
        } else {
            expired.setText(info.get(SignManager.EXPIRED));
        }
    }
}
