package com.cygnusa.hyperm;

import android.annotation.SuppressLint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.wrappers.Bulletin;
import com.cygnusa.hyperm.wrappers.CommonResponse;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BulletinDetails extends AppCompatActivity {
    public static Bulletin CONTENT = new Bulletin();
    TextView title, desc, publishedDate, headtitle;
    ImageView image;
    private String bulletinId;

    private ApiService apiService;

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

        setContentView(R.layout.activity_bulletin_details);
        setupWindowAnimations();

        headtitle = findViewById(R.id.title);
        title = findViewById(R.id.bulletinTitle);
        desc = findViewById(R.id.bulletinDescription);
        publishedDate = findViewById(R.id.publishedDate);
        image = findViewById(R.id.myImage);
        //  mImageLangSelection = findViewById(R.id.imgLanguage);
        publishedDate.setText("Published On: " + dateFormate(CONTENT.getPublished_date()));
        title.setText(CONTENT.getBulletin_title());
        headtitle.setText("Bulletin");
        desc.setText(CONTENT.getBulletin_desc());
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.back);
        toolbar.setContentInsetStartWithNavigation(0);
        toolbar.setTitle("");
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        @SuppressLint("PrivateResource")
        Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
        backArrow.setColorFilter(getResources().getColor(R.color.btn_bg_color), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(backArrow);
        apiService = ((MainApplication) getApplication()).getClient();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            bulletinId = bundle.getString("bulletinId");
        }

        sendBulletinIdUpdate();

        findViewById(R.id.helpIcon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        Glide.with(this).load(CONTENT.getImage_url())
                .thumbnail(Glide.with(this).load(R.drawable.gif_loader))
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
                        .placeholder(R.drawable.placeholder)).into(image);
    }

    private void sendBulletinIdUpdate() {
        apiService.sendBulletinUpdate(bulletinId).enqueue(new Callback<CommonResponse>() {
            @Override
            public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                if (response.body() != null) {
                    Log.e("BulletinResponse", response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<CommonResponse> call, Throwable t) {
                if (t.getMessage() != null)
                    Log.e("BulletinFailedResponse", t.getMessage());
            }
        });
    }

    private void setupWindowAnimations() {
        this.overridePendingTransition(R.anim.animation_enter,
                R.anim.animation_leave);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            confirmClose();
        }
        return super.onOptionsItemSelected(item);
    }

    private void confirmClose() {
        finish();
        BulletinDetails.this.overridePendingTransition(R.anim.animation_f_enter,
                R.anim.animation_f_leave);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.overridePendingTransition(R.anim.animation_f_enter,
                R.anim.animation_f_leave);
    }

    public String dateFormate(String dte) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newDate = null;
        try {
            newDate = format.parse(dte);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        format = new SimpleDateFormat("dd-MMM-yyyy");
        String date = format.format(newDate);


        return date;
    }

}
